Home > Android > Android practice-ListView SimpleAdapter

Android practice-ListView SimpleAdapter

References: SimpleAdapter

Public Constructors
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

Display elements in a list is a very common and useful in mobile applications.
The following shows an example of  use SimpleAdapter by custom row in an Activity
1, The simpleadapter.xml  contains only one ListView control .
</pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

<ListView
 android:id="@+id/list"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:divider="@null"
 android:dividerHeight="0dip" />

</LinearLayout>
<pre>

2, create  item.xml to define your custom row layout for each element of the listview

</pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >

<ImageView
 android:id="@+id/userIcon"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_margin="8dp" >
 </ImageView>

<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginBottom="5dp"
 android:layout_marginTop="5dp"
 android:orientation="vertical"
 android:paddingLeft="0px"
 android:paddingRight="5dp" >

<RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" >

<TextView
 android:id="@+id/username"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:textColor="#FFF38585"
 android:textSize="15sp" >
 </TextView>
 </RelativeLayout>

<TextView
 android:id="@+id/usertext"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="4dp"
 android:textColor="#FF2CE4A4"
 android:textSize="13sp" >
 </TextView>
 </LinearLayout>

</LinearLayout>
<pre>

3,Then you can use SimpleAdapter to manage your datasource which  is a list of Maps. The Map defines for each field in the item.xml and its mapping key.
public class SimpleAdapterActivity extends Activity {

private ListView mListView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
 setContentView(R.layout.simpleadapter);
 mListView = (ListView)findViewById(R.id.list);

List<Map> data = GetSampleData();
 SimpleAdapter adapter = new SimpleAdapter(this, data,
 R.layout.item, new String[] { "userIcon", "username", "usertext" },
 new int[] { R.id.userIcon, R.id.username, R.id.usertext });

mListView.setAdapter(adapter);
 }

List<Map> GetSampleData()
 {
 List<Map> list = new ArrayList<Map>();

Map map = new HashMap();
 map.put("userIcon", R.drawable.scott);
 map.put("username", "Shen");
 map.put("usertext", "This is a simple sample for SimpleAdapter");
 list.add(map);
 map = new HashMap();
 map.put("userIcon", R.drawable.ricardo);
 map.put("username", "Ricardo");
 map.put("usertext", "This is a simple sample for SimpleAdapter");
 list.add(map);

return list;
 }
}
5, Finally , if you run this example you would get the follow list.
simpleadaptersample

simpleadaptersample

SimpleAdapter would fine for most cases , However it may not be the best for your performance.
You have to convert your module data structure to “an arraylist of maps”.
Categories: Android
  1. October 30, 2012 at 7:23 am

    Nice article, simple and elegant.

  1. December 2, 2012 at 1:06 am

Leave a comment