Java.lang.String cannot be added to com.example.expandablelistview.NewsItem

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())); // Save the values of listDataChild } editor.commit(); 

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()) ); // Put it in listDataChild } 

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 )

+4
source share
4 answers

As others have already noted, the expectations of simply using Arrays.asList() and adding data to your list are incorrect and will not work. When you use SharedPreferences.Editor.putString() , you insert a String into the preferences representing the string returned by List.toString() (which will print all its children (using toString() ) in square brackets). When you return this line from the settings, you need to create it in the List of NewsItems . One way to do this would be as follows:

 for (Entry<String, ?> entry : pref.getAll().entrySet()) { List<NewsItem> rowItems = new ArrayList<NewsItem>(); // the string from the preferences as it was saved String prefContent = (String) entry.getValue(); // break against possible multiple NewsItems(I'm assuming that // 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() > 50) { // ; is the delimiter for the NewsItem 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); } 

In the above code, you will need to change the toString() method of the NewsItem class:

 @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(); } 
+2
source

You need to convert the String that you somehow return from SharedPreferences to the NewsItem list. Only the tide will not do this.

+2
source

this exception means that you are trying to assign a String object to the variable com.example.expandablelistview.NewsItem

I know this does not help, but in any way

+1
source

I'm not sure -

Is it possible to convert map values ​​to a list and put in a HashMap listDataChild?

How do you want to save POJO data in shared privileges. You can use the JSON format to serialize your NewsItemList and the objects it contains, and then save it as a string in SharedPreferences. Google JSON is a good api for this.

If you want to return data, extract a String and use a JSONArray to retrieve each object and add it to the new NewsItemList.

Otherwise, try to serialize your object to a private file . This will help you go beyond SharedPreferences putString (), putFloat (), putLong (), putInt () and putBoolean (). Thanks.

0
source

Source: https://habr.com/ru/post/1498717/


All Articles