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.

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>> list = new ArrayList<map>>(); // 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.
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
Thanks, you helped me a lot. Greetings from Germany.
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.
how to code its click event?
Maybe for a future tutorial, include something about click events to select a list item?
Thanks, this is exactly what I was looking for!
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..
The XML is missing.. might be helpful.. cheers.