Android 如何将数据从arraylist添加到共享首选项
我有一个异步任务,从服务器获取JSONArray格式的数据。我想将该数据保存在共享首选项中,并在列表视图中显示它。我正在使用适配器。 我试过了,但什么也没得到Android 如何将数据从arraylist添加到共享首选项,android,sharedpreferences,Android,Sharedpreferences,我有一个异步任务,从服务器获取JSONArray格式的数据。我想将该数据保存在共享首选项中,并在列表视图中显示它。我正在使用适配器。 我试过了,但什么也没得到 JSONArray arr = new JSONArray(strServerResponse); JSONObject jsonObj = arr.getJSONObject(0); for (int i = 0; i < arr.length(); i++) { pojo = new Pojo()
JSONArray arr = new JSONArray(strServerResponse);
JSONObject jsonObj = arr.getJSONObject(0);
for (int i = 0; i < arr.length(); i++) {
pojo = new Pojo();
JSONObject jobj2 = arr.getJSONObject(i);
String tipoftheday = jobj2.optString("tipsoftheday");
ArrayList<String> tii = new ArrayList<String>();
tii.add(tipoftheday);
}
JSONArray arr=新的JSONArray(strserver响应);
JSONObject jsonObj=arr.getJSONObject(0);
对于(int i=0;i
此每日小贴士包含多个数据,我希望将其保存在共享首选项中,然后在列表视图中显示。pojo是一个我定义了setter和getter的类
ArrayList<Pojo> tips;
tips = new ArrayList<Pojo>();
pojo.setTip(mydatafromsharedprefernces);
tips.add(pojo);
tipsAdapter = new TipsAdapter(TipsActivity.this, tips);
listTips.setAdapter(tipsAdapter);
arraylisttips;
tips=新的ArrayList();
setTip(mydatafromsharedprefernces);
提示:添加(pojo);
TipAdapter=新的TipAdapter(tipActivity.this,tips);
setAdapter(tipsAdapter);
如何在共享首选项中添加数据。有人能帮我吗。你提到你用json获取数据。将json字符串存储在共享首选项中。从共享首选项返回时对其进行解码。您提到过,您将以json格式获取数据。请将该json字符串存储在共享首选项中。当您从共享首选项返回时对其进行解码。希望您知道如何声明SharedReference对象以及如何使用编辑器对象 将arraylist转换为List 检索列表:
Set<String> set = editor.getStringSet("key", null);
Set=editor.getStringSet(“key”,null);
希望您知道如何声明SharedReference对象以及如何使用Editor对象
将arraylist转换为List
检索列表:
Set<String> set = editor.getStringSet("key", null);
Set=editor.getStringSet(“key”,null);
我们在“SharedReferences.Editor
”中有方法“putStringSet
”
putStringSet(字符串键,设置值)
您必须将ArrayList
转换为Set。
但是Pojo必须是可序列化的
Set<String> set = new HashSet<String>(list);
Set Set=newhashset(列表);
并使用上述方法
检查以下链接
我们在“SharedReferences.Editor
”中有方法“putStringSet
”
putStringSet(字符串键,设置值)
您必须将ArrayList
转换为Set。
但是Pojo必须是可序列化的
Set<String> set = new HashSet<String>(list);
Set Set=newhashset(列表);
并使用上述方法
检查以下链接
最好将JSON数据转换为字符串并存储在一个共享首选项中。最好将JSON数据转换为字符串并存储在一个共享首选项中。错误类型为2。找到:“java.util.List”,必需:“java.util.SetList listTemp=tii;Set temp=新哈希集(listemp);SharedReferences.Editor=GetSharedReferences(“MyPref”,MODE_PRIVATE).edit();编辑器.putStringSet(“tipoftheday”,temp);commit();现在如何在pojo classi中设置值我已经使用了pojo.setTip(set.toString());但我只得到了一个值错误的第二个参数类型。找到:“java.util.List”,必需:“java.util.SetList listTemp=tii;Set temp=新哈希集(listemp);SharedReferences.Editor=GetSharedReferences(“MyPref”,MODE_PRIVATE).edit();编辑器.putStringSet(“tipoftheday”,temp);commit();现在如何在pojo classi中设置值我已经使用了pojo.setTip(set.toString());但我只得到一个值
Set<String> set = new HashSet<String>(list);