0

I have been looking around for examples on how to do this. Cam across an approach that used SimpleAdapter and HashMaps here on StackOverflow. This is my code. It's not working. All I can see is an empty row of a list.

listItems = new ArrayList<HashMap<String,Object>>();

HashMap<String,Object> listData1 = new HashMap<String,Object>();
HashMap<String,Object> listData2 = new HashMap<String,Object>();

image_1 = getResources().getDrawable(R.drawable.camera_icon_focus_dim);
image_2 = getResources().getDrawable(R.drawable.camera_icon_scene_mode);

listData1.put("icon focus", image_1);
listData2.put("icon_scene", image_2);

listItems.add(listData1);
listItems.add(listData2);

SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItems,
  R.layout.image_list_item, new String[]{"icon_focus"}, 
  new int[]{R.id.list_view_item1});

optionsList.setAdapter(listItemAdapter);

image_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView   xmlns:android="http://schemas.android.com/apk/res/android"
        android:paddingTop="12dip"       
        android:layout_width="wrap_content"       
        android:layout_height="wrap_content"        
        android:id="@+id/listitem_img"
    />

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_view_item1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1.0"
/>

Please tell me what I'm doing wrong.

Benjol
  • 63,995
  • 54
  • 186
  • 268
Namratha
  • 16,630
  • 27
  • 90
  • 125

1 Answers1

4

The field in your HashMap needs to match. Try this:

listItems = new ArrayList<HashMap<String,Integer>>();
String fieldName = "icon_id";

HashMap<String, Integer> listData1 = new HashMap<String, Integer>();
HashMap<String, Integer> listData2 = new HashMap<String, Integer>();

listData1.put(fieldName, R.drawable.camera_icon_focus_dim);
listData2.put(fieldName, R.drawable.camera_icon_scene_mode);

listItems.add(listData1);
listItems.add(listData2);

SimpleAdapter listItemAdapter = new SimpleAdapter(
    this,
    listItems,
    R.layout.image_list_item,
    new String[] { fieldName },
    new int[] { R.id.listitem_img });
chiuki
  • 14,580
  • 4
  • 40
  • 38
  • It worked! Thank you :) But, I didn't understand why. Could you please explain? – Namratha Dec 07 '11 at 08:35
  • There are two parallel arrays passed to `SimpleAdapter`, one with `String` and one with `int`. Each position maps the key in the `HashMap` to a `View` specified in the template (`R.layout.image_list_item` in your case). The ListView fetches an item from the list, which is a `HashMap`, uses the key (`"icon_id"` in the example) to get the value to populate the `View` (`R.id.listitem_img` in the example). You can show multiple fields in your list item template by storing it with a different key, append that key to the `String[]` and append the corresponding view id to the `int[]` – chiuki Dec 07 '11 at 19:07
  • why is the text not adding on the listview? @chiuki – ajdeguzman Nov 29 '13 at 02:20