Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.lang.String cannot be cast to com.example.expandablelistview.NewsItem
    primarykey
    data
    text
    <p>I'm trying to save the expandablelistview child data to <a href="http://developer.android.com/reference/android/content/SharedPreferences.html" rel="nofollow noreferrer"><code>SharedPreferences</code></a>. The saving is working but when I try to load it then I get errors.</p> <p><strong>Problem:</strong></p> <p>When trying to load the settings and put it in a <a href="http://developer.android.com/reference/java/util/HashMap.html" rel="nofollow noreferrer">HashMap</a> I get these errors (shorted to 3 lines):</p> <pre><code>08-24 12:40:05.138: E/AndroidRuntime(807): FATAL EXCEPTION: main 08-24 12:40:05.138: E/AndroidRuntime(807): java.lang.ClassCastException: java.lang.String cannot be cast to com.example.expandablelistview.NewsItem 08-24 12:40:05.138: E/AndroidRuntime(807): at com.example.expandablelistview.ExpandableListAdapter.getChildView(ExpandableListAdapter.java:62) </code></pre> <p>For saving the data I'll use this code:</p> <pre><code> SharedPreferences pref = context.getSharedPreferences("myPrefs", MODE_PRIVATE); SharedPreferences.Editor editor= pref.edit(); for( Entry&lt;String, List&lt;NewsItem&gt;&gt; entry : listDataChild.entrySet() ) { editor.putString( entry.getKey(), String.valueOf(entry.getValue())); // Save the values of listDataChild } editor.commit(); </code></pre> <p>The output of the data is:</p> <pre><code>Yesterday: [[ headline=51.17346, 3.2252, Speed=2.10KM/H, Direction=158, date=18-8-2013 13:37:32]] Older: [[ headline=51.74346, 3.23252, Speed=2.00KM/H, Direction=122, date=17-8-2013 11:40:30]] Today: [[ headline=51.07346, 3.35252, Speed=0.00KM/H, Direction=122, date=19-8-2013 18:50:42]] </code></pre> <p>When trying to load these data (the errors happened here) and put it in a HashMap I'll use this (<a href="https://stackoverflow.com/questions/8151523/">from here</a>):</p> <pre><code> for( Entry&lt;String, ?&gt; entry : pref.getAll().entrySet() ) { listDataChild.put( entry.getKey(), (List&lt;NewsItem&gt;) Arrays.asList(entry.getValue()) ); // Put it in listDataChild } </code></pre> <p>The initialization of the data is happend here:</p> <pre><code>static void prepareListData(final Context context) { listDataHeader = new ArrayList&lt;String&gt;(); listDataChild = new HashMap&lt;String, List&lt;NewsItem&gt;&gt;(); // Adding child data listDataHeader.add("Today"); listDataHeader.add("Yesterday"); listDataHeader.add("Older"); List&lt;NewsItem&gt; today = new ArrayList&lt;NewsItem&gt;(); NewsItem newsData = new NewsItem(); newsData.setHeadline("51.07346, 3.35252"); newsData.setSpeed("0.00KM/H"); newsData.setDirection("122"); newsData.setDate("19-8-2013 18:50:42"); today.add(newsData); List&lt;NewsItem&gt; yesterday = new ArrayList&lt;NewsItem&gt;(); newsData = new NewsItem(); newsData.setHeadline("51.17346, 3.2252"); newsData.setSpeed("2.10KM/H"); newsData.setDirection("158"); newsData.setDate("18-8-2013 13:37:32"); yesterday.add(newsData); List&lt;NewsItem&gt; older = new ArrayList&lt;NewsItem&gt;(); newsData = new NewsItem(); newsData.setHeadline("51.74346, 3.23252"); newsData.setSpeed("2.00KM/H"); newsData.setDirection("122"); newsData.setDate("17-8-2013 11:40:30"); older.add(newsData); listDataChild.put(listDataHeader.get(0), today); listDataChild.put(listDataHeader.get(1), yesterday); listDataChild.put(listDataHeader.get(2), older); } </code></pre> <p><strong>Tried attempts:</strong></p> <p>I've tried to change the <code>listdataChild.put</code> to load the data to this:</p> <pre><code>listDataChild.put( entry.getKey(), new ArrayList&lt;NewsItem&gt;((Collection&lt;? extends NewsItem&gt;) Arrays.asList(entry.getValue())) ); </code></pre> <p>And to this:</p> <pre><code>listDataChild.put( entry.getKey(), (List&lt;NewsItem&gt;) new ArrayList(Arrays.asList(entry.getValue())) ); </code></pre> <p>But without any results. (I've got the same error)</p> <p><strong>Question:</strong></p> <p>Is it possible to convert the map values to list and put it in the HashMap <code>listDataChild</code>? </p> <p>Or should I use <code>newsData.setHeadline("Value");</code>, <code>newsData.setSpeed("Value");</code> etc.? (I don't know how to obtain specific values that's in <code>SharedPreferences</code> list)</p> <p><sup>(The source of <code>NewsItem.java</code> can be found <a href="http://pastebin.com/FcYHba51" rel="nofollow noreferrer">here</a>, the source of <code>ExpandableListAdapter.java</code> can be found <a href="http://pastebin.com/gGkZTkXX" rel="nofollow noreferrer">here</a>, and the source of <code>MainActivity.java</code> can be found <a href="http://pastebin.com/tXVJkfrL" rel="nofollow noreferrer">here</a>)</sup></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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