在共享preferences如何存储字符串数组中的Andr​​oid应用程序 [英] In shared preferences how to store string array in android application

查看:155
本文介绍了在共享preferences如何存储字符串数组中的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序正在使用列表视图中的基本适配器。

In my application am using list view in base adapter.

当我点击了这个项目的ID存储在共享preferences字符串数组格式。如何保存多个项目的id字符串数组格式 [1,2,5,6]这样的....

when i click the item its id store in shared preferences string array format. how to save multiple item id in string array format [1,2,5,6] like this....

在此先感谢...

推荐答案

您可以尝试使用 JSONArray 作为JSON是重量轻此外,您还可以创建一个 JSONArray ,并将其写入共享preference为String。

You can try using JSONArray as JSON is light-weight also, you can create a JSONArray and write it to SharedPreference as String.

写的,

       SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        JSONArray jsonArray = new JSONArray();
        jsonArray.put(1);
        jsonArray.put(2);
        Editor editor = prefs.edit();
        editor.putString("key", jsonArray.toString());
        System.out.println(jsonArray.toString());
        editor.commit();

阅读,

        try {
            JSONArray jsonArray2 = new JSONArray(prefs.getString("key", "[]"));
            for (int i = 0; i < jsonArray2.length(); i++) {
                 Log.d("your JSON Array", jsonArray2.getInt(i)+"");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

这篇关于在共享preferences如何存储字符串数组中的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆