【问题标题】:Inserting an Array in shared-preferences在共享首选项中插入数组
【发布时间】:2018-07-29 16:44:25
【问题描述】:

嘿,伙计,我有一个应用程序正在执行,它可以加载 Android 电视视图的电影。

我有一个站点,其中包含用于登录和内容的数据库中的所有信息。

所以在leanback中,类别已经定义为:

public static final String MOVIE_CATEGORY[] = {
        "Category Zero",
        "Category One",
        "Category Two",
        "Category Three",
        "Category Four",
        "Category Five",
};

但我想从我的数据库中获取信息。

所以我主要创建了 AsyncHttpTask 任务并解析我的响应,我的目标是在我的共享首选项中插入类别并从 MOVIE_CATEGORY 中检索它。

我只需要id和名字

   private void parseResult(String result) {

        try {
            JSONArray jsonarray = new JSONArray(result);

            SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();

            for(int i=0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String id       = jsonobject.getString("CID");
                String name    = jsonobject.getString("name");

 //Log.e("TAG","This is the results" + " ID "+ id + " Name "+name);
                iD = new String[]{id};
                catName=new String[]{name};
                editor.putString("id" + i, iD[i] + "name"+ catName[i] );
                editor.apply();
      Log.e("TAG","This is the results"  + Arrays.deepToString(iD));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

但我明白了

键:id0 值:6name2018

我的 json 响应如下所示:

    [{"CID":6,"name":"2018","image":"https:\/\/i.ytimg.com\/vi\/zTF913m8jVc\/maxresdefault.jpg"},
{"CID":7,"name":"2017","image":"http:\/\/mtltimes.ca\/wp-content\/uploads\/2017\/12\/Best-movies-2017-493x300.jpg"}

【问题讨论】:

    标签: android arrays json


    【解决方案1】:

    您不能将数组直接保存到共享首选项中。但是您可以保存 json 字符串并在需要时检索它,然后使用 Gson() 将其转换回数组。
    这是一个库,它允许您简单地将 json 对象转换为自定义类对象,并将 json 字符串转换为对象。 https://github.com/google/gson/blob/master/README.md

    【讨论】:

    • 好的,我会按原样存储它,但我需要检索它并再次解析它。这不是多余的吗?我可以先解析它获取名称和ID并仅存储它吗?
    • @Deepak 你可以使用putStringSet保存Set&lt;String&gt;
    【解决方案2】:
        /**
         * Set a set of String values in the preferences editor, to be written
         * back once {@link #commit} or {@link #apply} is called.
         * 
         * @param key The name of the preference to modify.
         * @param values The set of new values for the preference.  Passing {@code null}
         *    for this argument is equivalent to calling {@link #remove(String)} with
         *    this key.
         * @return Returns a reference to the same Editor object, so you can
         * chain put calls together.
         */
        Editor putStringSet(String key, @Nullable Set<String> values);
    
    
    public static void storeSharedItem(Context ctx, String key, Set<String> stringSet) {
        SharedPreferences sharedPref = ctx.getSharedPreferences(
                Constants.WOK_SHARED_PREF, 0);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putStringSet(key, stringSet);
        editor.apply();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2011-08-07
      • 2018-03-20
      • 2018-04-15
      • 2017-08-19
      • 2012-09-08
      相关资源
      最近更新 更多