【问题标题】:how to store 2dimensional array in shared preferences in android or serialize it如何在android的共享首选项中存储二维数组或序列化它
【发布时间】:2014-06-09 07:17:42
【问题描述】:
i am developing an android app where i have to save dates (* as given in array)
i am doing this by using shared preferences 
my code is working fine but it do some different behaviour which i dont know why
  a.> when i am saving array in shared prefrences it works perfect as i want but 
  b.> when it comes to fetching data it keeps single date  if the date occurs twice in      array ex.. 24 occurs 2 times in my array but on fetching it show me single 24
  c.> also it gives me unordered representation of array 

 i dont know why this is happening and how to solve it 
 **some one told me to use serialize but i dont know how to do this? **    
    String[][] my_date;
    my_date = new String[][] {
                {"14","26"},
                {"12","16","24","27"},
                {"17"},
                {"8","13","18"},
                {"14"},
                {},
                {"29"},
                {"15","18"},
                {},
                {"2","3","6","8","23"},
                {"4","6","24"},
                {}
        };

    /* storing the data in shared preferences */
        SharedPreferences pref = arg0.getSharedPreferences("MyPref", 0); // 0 - for private mode
        SharedPreferences.Editor editor = pref.edit();

            Set<String> set = new HashSet<String>();
               for(int i=0; i<my_date.length;i++){
                   for(int j =0; j<my_date[i].length;j++){
                       set.add("'"+my_date[i][j]+"'"); 
                       Log.v("dates",my_date[i][j]);
                   }
               }
               editor.putStringSet("dates", set);
               editor.commit();

/* fetching saved data */
     set=pref.getStringSet("dates", set);
     Log.v("dates",set+"----"+set.size());  

【问题讨论】:

    标签: android serialization sharedpreferences


    【解决方案1】:

    首先:这是一个坏主意。您可能更愿意以某种方式存储东西,以便您可以对它们进行部分编辑,例如。就像使用您自己的自定义 SQLLite 数据库一样。

    但是,如果您对 hacky 方法感到满意,并且希望将该多维数组存储在共享首选项中,您可能只需将其序列化为字符串并将该字符串存储在共享首选项中。

    你可能只是从这个例子中借用了一些关于如何将事物序列化为字符串的部分: Reliably convert any object to String and then back again

    然后获取该字符串并将其存储在共享首选项中。

    编辑:哦,好吧,我很好奇,所以我试了一下。我实际上发现我也必须对字符串进行 base64 编码,我希望我能以一种连贯的方式解释原因,但这大致就是我所做的:

    private void mySharedPreferencesThing() {
        String[][] before = new String[][] {{"1","2","3"}, {}, {"4","5","6"}};
    
        System.out.println("String thing before: " + arrayToString(before));
    
        SharedPreferences sp = getSharedPreferences("lolcats", MODE_PRIVATE);
        String[][] after = null;
        try {
            sp.edit().putString("cats", twoDimensionalStringArrayToString(before)).commit();
            after = stringToTwoDimensionalStringArray(sp.getString("cats", null));
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        System.out.println("String thing after: " + arrayToString(after));
    }
    
    // Just for debugging and knowing that it looks correct - there may be
    // something built in to do this
    private String arrayToString(String[][] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("{\n");
        for (int i = 0; i < arr.length; i++) {
            sb.append("\t{" + TextUtils.join(", ", arr[i]) + "}");
            if (i != arr.length - 1) { sb.append(","); }
            sb.append("\n");
        }
        sb.append("}");
        return sb.toString();
    }
    
    private String twoDimensionalStringArrayToString(String[][] s) throws UnsupportedEncodingException, IOException {
        ByteArrayOutputStream bo = null;
        ObjectOutputStream so = null;
        Base64OutputStream b64 = null;
        try {
            bo = new ByteArrayOutputStream();
            b64 = new Base64OutputStream(bo, Base64.DEFAULT);
            so = new ObjectOutputStream(b64);
            so.writeObject(s);
            return bo.toString("UTF-8");
        } finally {
            if (bo != null) { bo.close(); }
            if (b64 != null) { b64.close(); }
            if (so != null) { so.close(); }
        }
    }
    
    private String[][] stringToTwoDimensionalStringArray(String s) throws ClassNotFoundException, IOException {
        ByteArrayInputStream bi = null;
        ObjectInputStream si = null;
        Base64InputStream b64 = null;
        try {
            byte b[] = s.getBytes("UTF-8"); 
            bi = new ByteArrayInputStream(b);
            b64 = new Base64InputStream(bi, Base64.DEFAULT);
            si = new ObjectInputStream(b64);
            return (String[][]) si.readObject();
        } finally {
            if (bi != null) { bi.close(); }
            if (b64 != null) { b64.close(); }
            if (si != null) { si.close(); }
        }
    }
    

    【讨论】:

    • 我离目的地很近,但不知道如何实现代码
    • 我编辑了我的答案,把代码递给了你一个银盘:) 我认为这行得通。祝你好运!
    • SharedPreferences sp = getSharedPreferences("lolcats", MODE_PRIVATE);你能解释一下这个“MODE_PRIVATE”给我错误吗
    • 抱歉,将其更改为 context.getSharedPreferences("lolcats", Context.MODE_PRIVATE);` - 请参阅:developer.android.com/reference/android/content/… 您的示例中也有相同的内容,只是使用了硬编码0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多