Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The simplest solution is probably to substitute the <code>ArrayAdapter</code> and the <code>android.R.layout.simple_list_item_1</code> that you are using with a <code>SimpleAdapter</code> and the <code>android.R.layout.simple_list_item_2</code> predefined layout. This layout is composed by two <code>TextView</code>s, with an id of <code>android.R.id.text1</code> (the "item") and <code>android.R.id.text2</code> (the "sub item") respectively, which you will need as a reference for the <code>SimpleAdapter</code> to work.</p> <p>By <a href="http://developer.android.com/reference/android/widget/SimpleAdapter.html#SimpleAdapter%28android.content.Context,%20java.util.List%3C?%20extends%20java.util.Map%3Cjava.lang.String,%20?%3E%3E,%20int,%20java.lang.String%5B%5D,%20int%5B%5D%29" rel="noreferrer">looking at the constructor</a> for <code>SimpleAdapter</code> you will notice that, apart from a <code>Context</code> instance and the id of a layout resource, it takes three parameters that may be new to you:</p> <ul> <li>a <code>List&lt;? extends Map&lt;String, ?&gt;&gt;</code> instance where you put the elements you want the <code>ListView</code> to show. Elements are in the form of a <code>Map</code>, i.e. something akin to a struct composed by properties in form of name/value pairs. For example, you may use <code>"title"</code> and <code>"date"</code> as keys for the title and date of each RSS item, respectively.</li> <li>an array of strings, where to put the names of the keys in each map that you want to show on the <code>ListView</code>.</li> <li>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 <code>new String[] { "title", "date" }</code> as the array of strings argument, and <code>new int[] { android.R.id.text1, android.R.id.text2 }</code> as this argument.</li> </ul> <p>A rough code example, just to give you the idea:</p> <pre><code>List&lt;Map&lt;String, String&gt;&gt; data = new ArrayList&lt;Map&lt;String, String&gt;&gt;(); for (RSSItem item : feed.getAllItems()) { Map&lt;String, String&gt; datum = new HashMap&lt;String, String&gt;(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); </code></pre> <p>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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload