2

I have an expanded list, that is being made from HashMap<String, List<String>> , I would like to know if there is some kind possibility to store that value to preferences or something, so user could edit expanded list entries. Any information, suggestions will be appreciated.

Currently I am creating list in program and it is not saved to any prefs:

public class MainActivity extends Activity{
        ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        HashMap<String, List<String>> listDataChild;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);




            // get the listview
            expListView = (ExpandableListView) findViewById(R.id.lvExp);

            // preparing list data
            prepareListData();

            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

            // setting list adapter
            expListView.setAdapter(listAdapter);


        }

        /*
         * Preparing the list data
         */
        private void prepareListData() {
            listDataHeader = new ArrayList<String>();
            listDataChild = new HashMap<String, List<String>>();

            // Adding header data
            listDataHeader.add("Vegetables");
            listDataHeader.add("Fruits");
            listDataHeader.add("Drinks");

            // Adding child data
            List<String> vegetables = new ArrayList<String>();
            vegetables.add("Tomatoes");
            vegetables.add("Potatoes");
            vegetables.add("Cucumbers");
            vegetables.add("Pumpkins");
            vegetables.add("Peppers");
            vegetables.add("Onions");
            vegetables.add("Garlic");

            List<String> fruits = new ArrayList<String>();
            fruits.add("Strawberries");
            fruits.add("Blackcurrants");
            fruits.add("Redcurrant");
            fruits.add("Gooseberry");
            fruits.add("Kiwifruit");
            fruits.add("Grape");

            List<String> drinks = new ArrayList<String>();
            drinks.add("Vodka");
            drinks.add("Milk");
            drinks.add("Water");
            drinks.add("CocaCola");
            drinks.add("Sprite");

            listDataChild.put(listDataHeader.get(0), vegetables); // Header, Child data
            listDataChild.put(listDataHeader.get(1), fruits);
            listDataChild.put(listDataHeader.get(2), drinks);
        }


        private String does(String child) {
            Intent intent = new Intent(this, Confirmation.class);
            intent.putExtra("key", child);
            startActivity(intent);
            return null;
        }

}

LOG

09-29 17:21:42.039: E/dalvikvm(25076): Could not find class 'com.google.gson.Gson', referenced from method lt.whiteGroup.ultimateshoppinglist.MainActivity.prepareListData
09-29 17:21:42.125: E/AndroidRuntime(25076): FATAL EXCEPTION: main
09-29 17:21:42.125: E/AndroidRuntime(25076): java.lang.NoClassDefFoundError: com.google.gson.Gson
09-29 17:21:42.125: E/AndroidRuntime(25076):    at lt.whiteGroup.ultimateshoppinglist.MainActivity.prepareListData(MainActivity.java:117)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at lt.whiteGroup.ultimateshoppinglist.MainActivity.onCreate(MainActivity.java:45)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at android.os.Looper.loop(Looper.java:130)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at android.app.ActivityThread.main(ActivityThread.java:3687)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at java.lang.reflect.Method.invoke(Method.java:507)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-29 17:21:42.125: E/AndroidRuntime(25076):    at dalvik.system.NativeStart.main(Native Method)
whiteLT
  • 338
  • 7
  • 21

1 Answers1

4

You have pretty complicated structure so in your case I prefer to use Gson library to serialize (convert) HashMap<String, List<String>> to String and store as basic String to preferences.

As example:

Gson gson = new Gson();
String str = gson.toJson(listDataChild);

After store str as String

To fetch it back, extract String from Preferences as single String and load your Map like:

Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, List<String>>>(){}.getType();
HashMap<String, List<String>> map = gson.fromJson(str, type);

By this way you can store any class and get it back on next load.

As a side note

I use this technique to store all application configuration to preferences as handler on each change. By this way if crash will happen, data configured by user will be saved.

But if you still want to store map, see this answer in SO

Community
  • 1
  • 1
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • Thank you very much, you suggested a sound solution :). I will try to adapt it and write to you ASAP. – whiteLT Sep 29 '13 at 13:39
  • By the way if you have time, could you please explain what Gson and Json is? – whiteLT Sep 29 '13 at 13:43
  • Json is language that used to convert/extract any variable from/to String. When Gson is package that able to convert straight all your class to String – Maxim Shoustin Sep 29 '13 at 13:52
  • Thank you, because I have `listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);`, I will have to use gson and json on both `listDataHeader``listDataChild`, or there is easier way? – whiteLT Sep 29 '13 at 14:01
  • By the way what do I need for Gson and Json, because my eclipse does not suggest me anything to import – whiteLT Sep 29 '13 at 14:03
  • The easy way to use Gson. with 2-3 rows you can convert your class to String – Maxim Shoustin Sep 29 '13 at 14:03
  • add `gson-1.7.1.jar` to your project – Maxim Shoustin Sep 29 '13 at 14:04
  • You mean, I can convert all my class i.e. `MainActivity` and have like saved instance of it? And then next time just to load it back? – whiteLT Sep 29 '13 at 14:14
  • actually Activity you can but you can't load it because you get on start new instance. We use it mostly for data Objects. You can google – Maxim Shoustin Sep 29 '13 at 14:16
  • Do I need to define gson class in manifest? I got force close saying `coud not find class` – whiteLT Sep 29 '13 at 14:23
  • I did so, and then I was able to import and compile. Error log updated above – whiteLT Sep 29 '13 at 14:25
  • go to project -> right click ->Properties -> Java build path -> Order and export Tab, select there checkbox with your jar, after clean project and run again – Maxim Shoustin Sep 29 '13 at 14:27