The simplest solution is probably to substitute the ArrayAdapter
and the android.R.layout.simple_list_item_1
that you are using with a SimpleAdapter
and the android.R.layout.simple_list_item_2
predefined layout. This layout is composed by two TextView
s, with an id of android.R.id.text1
(the “item”) and android.R.id.text2
(the “sub item”) respectively, which you will need as a reference for the SimpleAdapter
to work.
By looking at the constructor for SimpleAdapter
you will notice that, apart from a Context
instance and the id of a layout resource, it takes three parameters that may be new to you:
- a
List<? extends Map<String, ?>>
instance where you put the elements you want theListView
to show. Elements are in the form of aMap
, i.e. something akin to a struct composed by properties in form of name/value pairs. For example, you may use"title"
and"date"
as keys for the title and date of each RSS item, respectively. - an array of strings, where to put the names of the keys in each map that you want to show on the
ListView
. - an array of integers where you need to put the ids of the parts in the list item view where you want the single elements referenced by the keys in the preceding array of strings to be shown. For example, if you want to show the title and date of a RSS item in the “item” and “sub item” views respectively, you use
new String[] { "title", "date" }
as the array of strings argument, andnew int[] { android.R.id.text1, android.R.id.text2 }
as this argument.
A rough code example, just to give you the idea:
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (RSSItem item : feed.getAllItems()) {
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("title", item.getTitle());
datum.put("date", item.getDate().toString());
data.add(datum);
}
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2,
new String[] {"title", "date"},
new int[] {android.R.id.text1,
android.R.id.text2});
itemList.setAdapter(adapter);
The documentation states that “the maps contain the data for each row, and should include all the entries specified in the from parameter”, so both title and date should always be present.
Please note that this is all off the top of my head. I haven’t actually tested all the code, so you may very well encounter some quirk or bug that you need to adjust or fix on your way up.