嘿,我有一个正在执行的应用程序,可加载电影的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"}
答案 0 :(得分:0)
您不能将数组直接保存到共享首选项中。但是您可以保存json字符串并在需要时检索它,然后使用Gson()将其转换回数组。
这是允许您简单地将json对象转换为自定义类对象以及json字符串转换为对象的库。
https://github.com/google/gson/blob/master/README.md
答案 1 :(得分:0)
/**
* 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();
}