【问题标题】:not getting data from shared preferences没有从共享偏好中获取数据
【发布时间】:2015-09-14 12:27:05
【问题描述】:

我有异步任务,我从服务器获取 JSONArray 格式的数据。我想将该数据保存在共享首选项中并在列表视图中显示。我正在使用适配器。

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);
    List<String> listTemp = tii;
    Set<String> temp = new HashSet<String>(listTemp);
    SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    for (int m=0; m<listTemp.size(); m++){
            temp.addAll(listTemp);
            editor.putStringSet("tipoftheday",temp);
            editor.commit();
    }

我正在检索以下代码中的值。

SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
Set<String> set = prefs.getStringSet("tipoftheday", null);
 for(String p : set)
 {
       pojo.setTip(p);
       tips.add(pojo);

 }
 tipsAdapter = new TipsAdapter(TipsActivity.this, tips);
 listTips.setAdapter(tipsAdapter);

但我只得到一个值。代码有什么问题。谁能帮帮我。

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:

    问题是,当您存储数据时,每次创建一个新的ArrayList

    这里:

    ArrayList<String> tii = new ArrayList<String>();
    

    而且您在外部for 循环的每次迭代中都会获得首选项,您不必这样做。只需在循环之外获取引用并在需要时使用它。

    这里:

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

    试着把你的代码改成这样:

    JSONArray arr = new JSONArray(strServerResponse);
    JSONObject jsonObj = arr.getJSONObject(0);
    
    SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    ArrayList<String> tii = new ArrayList<String>();
    
    
    for (int i = 0; i < arr.length(); i++) {
        pojo = new Pojo();
        JSONObject jobj2 = arr.getJSONObject(i);
        String tipoftheday = jobj2.optString("tipsoftheday");
    
        tii.add(tipoftheday);
    }
    
    List<String> listTemp = tii;
    Set<String> temp = new HashSet<String>(listTemp);
    
    temp.addAll(listTemp);
    editor.putStringSet("tipoftheday",temp);
    editor.commit();
    

    编辑:在代码的“检索”部分,您忘记实例化 Pojo 对象。

    喜欢:

    pojo = new Pojo();
    

    所以你的最后一个for 循环应该类似于:

    for(String p : set) {
       pojo = new Pojo();
       pojo.setTip(p);
       tips.add(pojo);
    }
    

    【讨论】:

    • 谢谢.. 我弄错了。但问题是只有一个值被检索了两次,最后一次添加的不是两个不同的值
    • 仍然无法正常工作.. 检索共享首选项是否有任何错误
    • 会看看。等等。
    【解决方案2】:

    您的代码中的问题是您每次都在优先设置 Set 值,即在每个元素的循环中,这就是为什么您只能获取最后输入的值的原因。更改以下代码

     Set<String> temp = new HashSet<String>(listTemp);
        SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
        for (int m=0; m<listTemp.size(); m++){
                temp.addAll(listTemp);
                editor.putStringSet("tipoftheday",temp);
                editor.commit();
        }
    

    用下面的替换它

    Set<String> temp = new HashSet<String>(listTemp);
        SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
        for (int m=0; m<listTemp.size(); m++){
                temp.addAll(listTemp);
        }
    editor.putStringSet("tipoftheday",temp);
    editor.commit();
    

    看看它是否有效

    编辑:-

    将添加代码更改为

    JSONArray arr = new JSONArray(strServerResponse);
    JSONObject jsonObj = arr.getJSONObject(0);
    ArrayList<String> tii = new ArrayList<String>();
    for (int i = 0; i < arr.length(); i++) {
        pojo = new Pojo();
        JSONObject jobj2 = arr.getJSONObject(i);
        String tipoftheday = jobj2.optString("tipsoftheday");
        tii.add(tipoftheday);
    
        }
    
    List<String> listTemp = tii;
    Set<String> temp = new HashSet<String>(listTemp);
    SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    temp.addAll(listTemp);
    editor.putStringSet("tipoftheday",temp);
    editor.commit();
    

    并检索代码到

    SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
    Set<String> set = prefs.getStringSet("tipoftheday", null);
     for(String p : set)
     {
           pojo = new Pojo();
           pojo.setTip(p);
           tips.add(pojo);
    
     }
     tipsAdapter = new TipsAdapter(TipsActivity.this, tips);
     listTips.setAdapter(tipsAdapter);
    

    看看它是否有效

    【讨论】:

    • 出错了,但只有一个值被检索了两次,最后一次添加的不是两个不同的值
    猜你喜欢
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 2012-05-01
    相关资源
    最近更新 更多