我想使用SimpleAdapter而不是自定义ArrayAdapter。除了与每行关联的图标之外,一切正在工作。该图标直接与标签相关。因此,根据标签,图标可能会有所不同。可以在SimpleAdapter中设置来自XML的动态图标吗?
下面是示例XML
<profile>
<id>16</id>
<name>Random Name</name>
<site>Random URL</site>
<icon>R.drawable.random_icon</icon>
</profile>
我定制行布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label" />
<ImageView
android:id="@+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow" />
</LinearLayout>
现在,这里是我解析我的XML和建立我的适配器(编辑只是相关部分):
NodeList children = doc.getElementsByTagName("profile");
for (int i = 0; i < children.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) children.item(i);
map.put("id", ParseXMLMethods.getValue(e, "id"));
map.put("name", ParseXMLMethods.getValue(e, "name"));
map.put("site", ParseXMLMethods.getValue(e, "site"));
map.put("icon", ParseXMLMethods.getValue(e, "icon"));
mylist.add(map);
}
View header = getLayoutInflater().inflate(R.layout.header, null, false);
ListAdapter adapter = new SimpleAdapter(this, mylist,
R.layout.rowlayout, new String[] { "name", "icon" }, new int[] { R.id.label, R.id.icon });
final ListView lv = getListView();
lv.addHeaderView(header, null, false);
lv.setSelector(R.color.list_selector);
setListAdapter(adapter);
该程序不会崩溃。一切似乎都是这样,我甚至解析“网站”,所以当点击行时打开了网页视图。我只是无法显示图标。这甚至可以用SimpleAdapter吗?
更新:这里是重写getView()方法:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int idImage = context.getResources().getIdentifier("icon","drawable", context.getPackageName());
// ????????
return view;
}
我不认为它会这样工作。您将需要制作一个自定义适配器,并在其中将图像分配给imageview – Akhil 2012-03-29 05:49:45