I would like to use a SimpleAdapter instead of customizing an ArrayAdapter. Everything is working except an icon that is associated with each row. The icon relates directly to the label. SO depending on the label, the icon may be different.
Here is sample XML
<profile>
<id>16</id>
<name>Random Name</name>
<site>Random URL</site>
<icon>R.drawable.random_icon</icon>
</profile>
My custom Row layout
<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>
Now here is where I Parse my XML and set up my Adapter (edited to just relevant parts):
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);
The program doesn’t crash. Everything appears as is, I even parse the “site” so when the row is clicked is opens up a web view. I just can’t get the icon to show. Is this even possible with SimpleAdapter?
UPDATE: Here is overriden getView() method:
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;
}
I can recommend you to achieve this is by using getIdentifier method from resources in an extended SimpleAdapter. But for that, first you need to change your xml a little:
Instead of saving programmable reference of image into your xml, you should simply save the file name:
Now, extend your SimpleAdapter to your own adapter class and override
getViewmethod. In that method you can re-construct resource id from the name and set it to your ImageView:Update
*this class code is a reference for you to see how your adapter should look like. The more appropriate way would be to use ArrayAdapter, but since you are already using SimpleAdapter so I’ve just extended its capabilities