I'm trying to save the expandablelistview child data to SharedPreferences. The saving is working but when I try to load it then I get errors.
Problem:
When trying to load the settings and put it in a HashMap I get these errors (shorted to 3 lines):
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)
For saving the data I'll use this code:
SharedPreferences pref = context.getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor= pref.edit();
for( Entry<String, List<NewsItem>> entry : listDataChild.entrySet() ) {
editor.putString( entry.getKey(), String.valueOf(entry.getValue())); // Save the values of listDataChild
}
editor.commit();
The output of the data is:
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]]
When trying to load these data (the errors happened here) and put it in a HashMap I'll use this (from here):
for( Entry<String, ?> entry : pref.getAll().entrySet() ) {
listDataChild.put( entry.getKey(), (List<NewsItem>) Arrays.asList(entry.getValue()) ); // Put it in listDataChild
}
The initialization of the data is happend here:
static void prepareListData(final Context context) {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<NewsItem>>();
// Adding child data
listDataHeader.add("Today");
listDataHeader.add("Yesterday");
listDataHeader.add("Older");
List<NewsItem> today = new ArrayList<NewsItem>();
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<NewsItem> yesterday = new ArrayList<NewsItem>();
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<NewsItem> older = new ArrayList<NewsItem>();
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);
}
Tried attempts:
I've tried to change the listdataChild.put to load the data to this:
listDataChild.put( entry.getKey(), new ArrayList<NewsItem>((Collection<? extends NewsItem>) Arrays.asList(entry.getValue())) );
And to this:
listDataChild.put( entry.getKey(), (List<NewsItem>) new ArrayList(Arrays.asList(entry.getValue())) );
But without any results. (I've got the same error)
Question:
Is it possible to convert the map values to list and put it in the HashMap listDataChild?
Or should I use newsData.setHeadline("Value");, newsData.setSpeed("Value"); etc.? (I don't know how to obtain specific values that's in SharedPreferences list)
(The source of NewsItem.java can be found here, the source of ExpandableListAdapter.java can be found here, and the source of MainActivity.java can be found here)