ListView without ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/title" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/artist" />
</LinearLayout>
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", "Poker Face");
map.put("artist", "Lady Gaga");
data.add(map);
map = new HashMap<String, String>();
map.put("title", "Yeah!");
map.put("artist", "Usher");
data.add(map);
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,
data,
R.layout.listview_without_images_view,
new String[]{ "title", "artist" },
new int[]{ R.id.title, R.id.artist });
ListView noImages = (ListView)findViewById(R.id.listViewNoImages);
noImages.setAdapter(simpleAdapter);
ListView with ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/title" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/artist" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView" />
</LinearLayout>
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", "Poker Face");
map.put("artist", "Lady Gaga");
map.put("pic", ((Integer)R.drawable.pic).toString());
data.add(map);
map = new HashMap<String, String>();
map.put("title", "Yeah!");
map.put("artist", "Usher");
map.put("pic", ((Integer)R.drawable.pic2).toString());
data.add(map);
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,
data,
R.layout.listview_with_images_view,
new String[]{ "title", "artist", "pic" },
new int[]{ R.id.title, R.id.artist, R.id.imageView });
simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder(){
@Override
public boolean setViewValue(View view, Object o, String s) {
if(view.getClass() == ImageView.class) {
ImageView image = (ImageView)view;
image.setImageResource(Integer.parseInt(s));
return true;
}
return false;
}
});
ListView withImages = (ListView)findViewById(R.id.listViewImages);
withImages.setAdapter(simpleAdapter);
Full code
https://github.com/randcode-generator/SimpleAdapterExample