如何在共享首选项android中添加多个值

时间:2014-01-17 11:44:44

标签: android

我有共同的偏好。我想存储列表视图中存在的多个值。

我收到数据但不是完整数据。

代码存在于基础适配器中,请帮助我,我的时间不多了。

Map<String, String> nameIcons = new HashMap<String, String>();
    nameIcons.put("publisheddate",publisheddate);
    nameIcons.put("validity",validity);
    nameIcons.put("accessstatus",accessstatus);

      settings=ctxt.getSharedPreferences("com.contentraven",0);
      editor=settings.edit();

      editor.clear();
      editor.commit();
      for (String s : nameIcons.keySet()) 
      {
          editor.putString(s, nameIcons.get(s));
      }

      editor.commit();

1 个答案:

答案 0 :(得分:0)

试试这段代码..

//For storing
      Map<String, String> nameIcons = new HashMap<String, String>();
      nameIcons.put("publisheddate",publisheddate);
      nameIcons.put("validity",validity);
      nameIcons.put("accessstatus",accessstatus);
      SharedPreferences pref = getContext().getSharedPreferences("com.contentraven",0);
      SharedPreferences.Editor editor = pref.edit(); 
      editor.clear();
      editor.commit();

      for (String s : nameIcons.keySet()) {
         editor.putString(s, map.get(s));
      }
      editor.commit();          

// for loading            
       SharedPreferences pref =   getContext().getSharedPreferences("com.contentraven",0);
       HashMap<String, String> map= (HashMap<String, String>) pref.getAll();
       int i=0;
       String val[]=new String[map.size()]; 
       for (String s : map.keySet()) {
            val[i]=map.get(s);

           i++;
        }