[英]How to I add an image to list view items in Android?
我正在尝试为每个列表项添加一个不同的图标,但我遇到了麻烦。 我们的想法是将每个列表视图项目放在一起以便于编辑,但添加图像比我想象的要复杂得多。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "title", "description" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Title 1", "Description 1"));
list.add(putData("Title 2", "Description 2"));
list.add(putData("Title 3", "Description 3"));
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
这不是不可能的,那么至少在您当前的设置中非常麻烦。
正如名称所述, SimpleAdapter是一个基本类,它为您提供基本功能。 通常是 - 带有文本视图的列表。
当您创建适配器时,您为其中的每个项目指定了一个确切的布局(这是您的android.R.layout_simple_list_item_2)。 你不能在那里推送任何其他物品(除非你真的很顽固)。
你需要什么:
这是一个很好的演示: http : //hmkcode.com/android-custom-listview-titles-icons-counter/
您可以在listview中使用自定义适配器。 所以可以自定义你的行。 检查一下
目前使用simple_list_item_2.xml进行ListView
。 这个布局只包含两个TextView。 因此无法使用simple_list_item_2.xml
布局在ListView行中显示图像
如何添加图像以列出Android中的视图项?
应该创建自定义适配器:
1.创建一个自定义布局,其中包含要在每个列表视图行中显示的所需视图,如TextView,ImageView,...
2.通过扩展SimpleAdapter
类来创建自定义适配器类,以更改getView
方法的行为,以便从数据源显示图像和textview
请参阅以下教程以供参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.