Using the SimpleAdapter with a ListView in Android

When writing the Call history part of my Android app for Mobile Vikings I wanted to display the calls and messages in a listview with their number, date/time and cost. Displaying some text values in a ListView isn’t that hard. I could use an ArrayAdapter object that takes an ArrayList of Calls and it would display the toString() method of each call in the ListView. What I wanted to do was display various attributes of each Call object (number, date, cost) in the ListView. Like this.

Calllog example

After some research I found out that the SimpleAdapter class was the way to go. The documentation on the SimpleAdapter class wasn’t very clear to me at first and there wasn’t a working codesample to demo the class so I decided to share my own code.

The SimpleAdapter class uses an ArrayList of Maps to hold the data. Each Map in the list represents the data for one row in the ListView. The mapping between the values in the Map and the TextViews in the layout file is done with two arrays. An array of Strings for the keys that need to be mapped and an array of ints with the id’s of the TextViews the data belongs to.

We’ll start of with the code for the layout file. This file describes how one row in our listview should look. I used a TableLayout because I wanted two columns. In the first column I have a LinearLayout with two TextViews. One for the number display and one for the time. The other column holds one TextView for the cost of the call.

 

Next up is the java code I used to convert my List of Calls to the List of Maps and to map the correct values to the corresponding TextViews.

// get messages from service
ArrayList smsMessages = service.getSmsCalls();
 
// initialize the List of Maps
List<map>&gt; list = new ArrayList<map>&gt;();
 
// iterate over all messages
// create a map for each message
// fill the map with data
for (Call c: smsMessages) {
	Map map = new HashMap();
	map.put("number", c.getTo());
	map.put("date", DateParser.getTimeString(c.getBegin()));
	map.put("price", "€ " + c.getPrice());
	list.add(map);
}
 
// the from array specifies which keys from the map
// we want to view in our ListView
String[] from = {"number", "date", "price"};
 
// the to array specifies the TextViews from the xml layout
// on which we want to display the values defined in the from array
int[] to = {R.id.callog_detail_sms_number, R.id.callog_detail_sms_date, R.id.callog_detail_sms_price};
 
// get a reference to the ListView
ListView lv = (ListView)findViewById(R.id.callog_detail_listview);
 
// create the adapter and assign it to the listview
SimpleAdapter adapter = new SimpleAdapter(this.getApplicationContext(), list, R.layout.callog_detail_sms, from, to);
lv.setAdapter(adapter);
</map></map>

That’s it. I hope this was helpfull to some of you. Improvements and comments are always welcome.

Share this article
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Twitter
  • del.icio.us
  • Reddit
  • RSS
This entry was posted in Android and tagged , , . Bookmark the permalink.

8 Responses to Using the SimpleAdapter with a ListView in Android

  1. griffin says:

    Hi Steven
    Do you have this sample ‘s project source code(java & xml).

    if YES, could you give me a copy. because i meet some problem to understand Adapter usage.

    Thanks a lot.

    By the way, my e-mail is:shijintao@gmail.com. Thanks

    /Griffin

  2. Fabian says:

    Thanks, you helped me a lot. Greetings from Germany.

  3. Brad says:

    Thank you so much! clear and concise, and almost exactly what I was looking for! I was really hoping to get an image into part of my table layout, this is a perfect start though. If you have any suggestions on images please let me know! If I come up with a solution I’ll be sure to share.

  4. soclose says:

    how to code its click event?

  5. binnyb says:

    Maybe for a future tutorial, include something about click events to select a list item?

  6. Thanks, this is exactly what I was looking for!

  7. Praveen says:

    Thanks… was wondering how to include more than one item in a single row in list view…and you have showed how.

    PS: this did not turn up in google search… stumbled on it when searching for something else..

  8. fusionstream says:

    The XML is missing.. might be helpful.. cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">