1

这个应用程序将从服务器获取儿子。我要使用的值是categoryid. 您在下面的代码中清楚地知道。

我如何使用categoryidjson 中的它来改变 imageview 的条件?

提示:我的 ImageView 在ImageAdapter课堂上

我想使用条件,

    if(catetgoryid == 1){
       imageview.setImageResource(R.drawable.image1)
    } elseif (categoryid == 2 {
        imageview.setImageResource(R.drawable.image2)
    } else {
     imageview.setImageResorce(R.drawable.image3)
    }

JSON

{
"count": 54,
"data": [
    {
        "userid": "24",
        "cardid": "114",
        "title": "meeting now!",
        "carddate": "2012-12-26",
        "categoryid": "2",
        "cardimage": "http://xxx.xxx/data/card/thum/9589266f7983dbe72ff8481800f51ef9.jpg",
        "public": "1",
        "user": {
            "user_id": "24",
            "firstname": "kong",
            "lastname": "kea",
            "email": "xxx",
            "picture": "http://xxx.xxx/data/profiles/thum/default.png",
            "friend_status": "NOT_FRIEND"
        }
    }
]

}

主要活动。这是获取 json 的 MainActivitycategoryid

@Override
    protected Void doInBackground(String... params) {

        String url = "http://xxx.xxx.xxx/card/all/30/0/?token=57LzEsmBeykLbDCD04wTgK9WWV2XjJY0XBdqVU0HQvjIdu5EtTWOT1IQ1AYNwxt6Q5bG6FG73uvzLQSDGAIezwc8VcopEp0s63uzbdVgLSfts0TLmuVDOgyfn4lX";
        JSONArray data = null;

        try {

            JSONObject jsonObject = new JSONObject(getJSONUrl(url));

            MyArrList = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> map;
            data = jsonObject.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);
                map = new HashMap<String, Object>();

                // Thumbnail Get ImageBitmap To Object
                map.put("cardimage", (String) c.getString("cardimage"));
                map.put("ImageThumBitmap",(Bitmap) loadBitmap(c.getString("cardimage")));

                map.put("categoryid",(String)c.getString("categoryid"));  // use it for condition 

                MyArrList.add(map);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

此处使用条件

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<HashMap<String, Object>> MyArr = new ArrayList<HashMap<String, Object>>();

    public ImageAdapter(Context c,ArrayList<HashMap<String, Object>> myArrList) {
        context = c;
        MyArr = myArrList;
    }

    public int getCount() {
        return MyArr.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        viewHolder = new ViewHolder();

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grid_item, null);
        }
            viewHolder.categoryCard = (ImageView) convertView.findViewById(R.id.category_card);

            // ====================== use codition here ===============
            viewHolder.categoryCard.setImageResource(R.drawable.card_beauty);  
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);

        try {
            viewHolder.imageView.setImageBitmap((Bitmap) MyArr.get(position).get("ImageThumBitmap"));
        } catch (Exception e) {
            viewHolder.imageView.setImageResource(android.R.drawable.ic_menu_report_image);
        }

        return convertView;

    }

}
4

2 回答 2

2

您将“catetgoryid”设置为字符串,然后进行整数比较。添加整数转换或将 if 更改为字符串比较。

于 2012-12-27T03:44:14.583 回答
1

在你的getView()方法中,尝试这样的事情。在下面的方法中,您将图像视图与 categoryid 一起发送: // 使用 Integer.parseInt(categoryId) 将类别 id 从 String 转换为整数;或者您可以在您的 if 条件中与字符串本身进行比较,例如 categoryId.equals("1"); 等等...

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        viewHolder = new ViewHolder();
........................
...........................

viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
putDefaultImage(categoryid, viewHolder.imageView);
.............................
}

 private void putDefaultImage(int categoryid, ImageView imageview)
    {
         if(catetgoryid == 1){
       imageview.setImageResource(R.drawable.image1)
    } elseif (categoryid == 2) {
        imageview.setImageResource(R.drawable.image2)
    } else {
     imageview.setImageResource(R.drawable.image3)
    }

希望这可以帮助。

于 2012-12-27T03:44:44.460 回答