Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As others have already pointed out your, expectations of simply using <code>Arrays.asList()</code> and casting to your data list is incorrect and will not work. As you use <code>SharedPreferences.Editor.putString()</code> you'll insert a <code>String</code> in the preferences representing the string returned by <code>List.toString()</code>(which will print all of its children(using <code>toString()</code>) in square brackets). When you get this string from the preferences back, you need to build it up into a <code>List</code> of <code>NewsItems</code>. One way of how you can do this would be like below:</p> <pre><code>for (Entry&lt;String, ?&gt; entry : pref.getAll().entrySet()) { List&lt;NewsItem&gt; rowItems = new ArrayList&lt;NewsItem&gt;(); // the string from the preferences as it was saved String prefContent = (String) entry.getValue(); // break against possible multiple NewsItems(I'm assuming that's // why you use a List of NewsItems) for the same // key (every NewsItem starts with * so we split after // that(see the NewsItems.toString method)) String[] newsItems = prefContent.split("\\*"); for (String item : newsItems) { // an item which doesn't have at least 50 characters // isn't a valid NewsItem string representation(based on // its toString() method) if (item.length() &gt; 50) { // ; is the delimiter for the NewsItem's data String[] components = item.split(";"); NewsItem ni = new NewsItem(); // for each of the four data components we split // after = and then use the second value obtained as // that will be the value for that component ni.setHeadline((components[0].split("="))[1]); ni.setSpeed((components[1].split("="))[1]); ni.setDirection((components[2].split("="))[1]); ni.setDate((components[3].split("="))[1]); rowItems.add(ni); } } listDataChild.put(entry.getKey(), rowItems); } </code></pre> <p>The code above would require a change in the <code>toString()</code> method of the <code>NewsItem</code> class:</p> <pre><code>@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("*headline="); sb.append(headline); sb.append(";"); sb.append("speed="); sb.append(speed); sb.append(";"); sb.append("direction="); sb.append(direction); sb.append(";"); sb.append("date="); sb.append(date); return sb.toString(); } </code></pre>
 

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