我想使用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