I am trying to save expandablelistview child data in SharedPreferences . Saving works, but when I try to load it, I get errors.
Problem:
When I try to load the settings and put them in the 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)
To save data, I will 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()));
Output:
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 this data (errors here) and put them in a HashMap, I will use this ( here ):
for( Entry<String, ?> entry : pref.getAll().entrySet() ) { listDataChild.put( entry.getKey(), (List<NewsItem>) Arrays.asList(entry.getValue()) );
Data initialization takes place 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); }
Trial Attempts:
I tried changing listdataChild.put to load data:
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 have the same error)
Question:
Is it possible to convert map values ββto a list and put in a HashMap listDataChild ?
Or use newsData.setHeadline("Value"); , newsData.setSpeed("Value"); etc.? (I don't know how to get specific values ββin the 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 )