Я бы хотел использовать SimpleAdapter вместо настройки ArrayAdapter. Все работает, кроме значка, который связан с каждой строкой. Значок относится непосредственно к этикетке. ТАК, в зависимости от метки, значок может отличаться.
Вот пример 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;
}