ListView与自定义backgroundResource

时间:2012-09-10 22:35:07

标签: android json listview

我需要在创建listview时使用自定义文本和自定义setbackgroundresource设置listview

这是我的代码:

listView通过JSON encdoe从URL接收数据:

{"results":[
    {"db_id":"6","discount":"active","db_description":"bla bla bla ","db_num":"137","db_num2":"260"},
    {"db_id":"14","db_type":"discount","db_description":"blaaaaaaa","db_num":"39","db_num2":"46"},
    {"db_id":"18","db_type":"discount","db_description":"blaaaaaaa","db_num":"335","db_num2":"456"},
]}

将数据添加到地图

            List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("type",RESULTS_PARAMS));
        JSONObject json = jsonParser.makeHttpRequest(RESULTS_URL, "GET",
                params);
        Log.d("JSON RESULT: ", json.toString());

        try {
            queues = json.getJSONArray(TAG_RESULTS);
            for (int i = 0; i < queues.length(); i++) {
                JSONObject c = queues.getJSONObject(i);

                String id = c.getString(TAG_ID);
                String type = c.getString(TAG_TYPE);
                String description = c.getString(TAG_DESCRIPTION);
                String num = c.getString(TAG_NUM);
                String num2 =  c.getString(TAG_NUM2);
                int imageint = getResources().getIdentifier(c.getString(TAG_TYPE) , "drawable", getPackageName());
                String image = String.valueOf(imageint);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_ID, id);
                map.put(TAG_TYPE, type);
                map.put(TAG_DESCRIPTION, description);
                map.put(TAG_NUM, num);
                map.put(TAG_NUM2, num2);
                map.put(TAG_IMAGE, image);

                QueueList.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

将DATA设置为listview

的代码
                    ListAdapter adapter = new SimpleAdapter(
                        QueueActivity.this,
                        QueueList,
                        R.layout.queue_row,
                        new String[] { TAG_ID, TAG_DESCRIPTION, TAG_NUM, TAG_NUM2, TAG_IMAGE},
                        new int[] { R.id.queueid, R.id.description, R.id.num, R.id.num2, R.id.list_image });

                setListAdapter(adapter);

现在我想将setBackgroundResource设置为R.drawable.customlistviewback 如果db_id(TAG_ID)= int info

对于ex,int info = 6;

1 个答案:

答案 0 :(得分:1)

要进行行级视图修改,您可能需要使用自定义适配器。通常,您会覆盖getView函数并在那里进行更改。请参阅how to customize listview row android以获得一个不错的例子。