0

创建列表视图时,我需要使用自定义文本和自定义 setbackgroundresource 设置列表视图

这是我的代码:

listView 通过 JSON 编码从 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();
        }

将数据设置到列表视图中的代码

                    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

例如,int info = 6;

4

1 回答 1

1

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

于 2012-09-10T22:44:52.017 回答