ArrayAdapterをカスタマイズする代わりに、SimpleAdapterを使用したいと思います。各行に関連付けられているアイコンを除いて、すべてが機能しています。アイコンはラベルに直接関連しています。そのため、ラベルによってはアイコンが異なる場合があります。
これがサンプル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);
プログラムはクラッシュしません。すべてがそのまま表示されます。「サイト」を解析することもできるので、行をクリックするとWebビューが開きます。アイコンを表示できません。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;
}