如何存储/保存HashMap <string,list <string =“”>&gt; </string,>

时间:2013-09-28 06:03:20

标签: android sharedpreferences expandablelistview

我有一个扩展列表,它来自HashMap<String, List<String>>,我想知道是否有某种可能性将该值存储到首选项或其他内容,因此用户可以编辑扩展列表条目。任何信息,建议将不胜感激。

目前我正在程序中创建列表,它不会保存到任何首字母:

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)

1 个答案:

答案 0 :(得分:3)

您的结构非常复杂,所以在您的情况下,我更喜欢使用Gson库来序列化(转换)HashMap<String, List<String>>String并将其作为基本字符串存储到首选项。

例如:

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

str存储为String

之后

要将其取回,请将“首选项”中的字符串从单个String中提取出来并加载Map,如:

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

通过这种方式,您可以存储任何类,并在下次加载时将其恢复。

作为旁注

我使用此技术将所有应用程序配置存储为首选项,作为每次更改的处理程序。通过这种方式,如果发生崩溃,将保存用户配置的数据。

但如果您仍想存储地图,请参阅SO中的this answer