0

I need to put small icons on the right of each list item. I know it can be done with a custom list but it will mean rewiring a lot of code. if there a way to do it without using a custom adapter?

mList = (ListView) findViewById(R.id.sidebar_list);
        mList.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, mStrings));
        mList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
4

3 回答 3

2

不,在自定义适配器内覆盖getView是为列表行动态设置不同图像的唯一方法。但是,如果您想为所有行项目设置静态(相同)图像,那么您可以为行定义自定义资源 xml(其中包含一个TextViewImageView或 comppund drawable-TextView)并在初始化时将其传递给您的默认适配器.

于 2012-09-08T20:49:37.820 回答
0

是的,您可以使用SimpleAdapter和带有 ImageView 的自定义布局。


简单的例子:

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("text", "one");
map.put("image", String.valueOf(getResources().getIdentifier("ic_launcher", "drawable", getPackageName())));
list.add(map);

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new SimpleAdapter(this, list, R.layout.list_item, new String[] {"text", "image"}, new int[] {R.id.text, R.id.image}));
于 2012-09-08T20:53:03.057 回答
0

您将需要为视图创建 XML 布局(如果您没有)并添加一个 imageview。你应该给它和ID('缩略图')。接下来你只需通过适配器的位置向适配器询问某个视图

View singleView = listView.getChildAt(wantedChild);
ImageView thumbnail = (ImageView) singleView.findViewById(R.id.thumbnail);
thumbnail.setImageBitmap(bitmap);
于 2012-09-08T20:53:28.600 回答