程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> string-用IF條件語句改變imageview

string-用IF條件語句改變imageview

編輯:編程綜合問答
用IF條件語句改變imageview

引用會從服務器得到son,我想要用的值是categoryid,比較詳細的在下邊的代碼中。
我怎麼才能用從json取得的 categoryid 來用條件改變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"
        }
    }
]}

MainActivity.這個是用來獲取json categoryid的

@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;

    }

}

最佳回答:


在你的getview()方法中,試試這樣。在下邊的方法中,你和categoryid一起發送了imageview://通過Integer.parseInt(categoryId);轉換the category id成integer,或者你可以在你的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.setImageResorce(R.drawable.image3)
    }

希望這個能幫到你

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved