【问题标题】:How to store string array (which is converted from array list <string> to string array) in shared preference.?如何在共享首选项中存储字符串数组(从数组列表 <string> 转换为字符串数组)。?
【发布时间】:2015-09-03 17:27:11
【问题描述】:

我有两个活动。我将数组列表从第二个活动传递到第一个活动。在第一个活动中,将数组列表转换为字符串数组。现在我想用共享首选项保存这个字符串数组,但我不能这样做。

传递数组列表的第二个活动代码

                Intent i = new Intent();
                //Bundle extra = new Bundle();
                //i.putStringArrayList("array",h);
                i.putStringArrayListExtra("array", h);
                //i.putExtras(extra);
                setResult(RESULT_OK,i);
                finish();

获取此结果的第一个活动代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

if(requestCode == 1){

    if(resultCode == RESULT_OK){

    //  Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
        File imgFile = null;
        Bundle extras = data.getExtras();

        a= extras.getStringArrayList("array");
        try{
        al = new String[a.size()];
        al = a.toArray(al);

        //for(String ss : al)             
              for(int i = 0; i < al.length; i++){
                  imagepathb.append(al[i]).append(",");

              } 
         }
         sharedpreferences.edit().putString("imgpath",imagepathb.tostring()).commit();                            catch(Exception e){

                  }

        if(ab != null){
            ab =  sharedpreferences.getString("imgpath", ab);
          Toast.makeText(getApplicationContext(), "This is ab image path : "+ ab, Toast.LENGTH_LONG).show();    
        }else{
            Toast.makeText(getApplicationContext(), "This is null", Toast.LENGTH_LONG).show();              
        }


    }

   }
}

我在使用这段代码时遇到了麻烦,因为当我尝试这段代码时,只有第一种语法开始执行,其他两种语法保持原样,Toast 显示空值。共享首选项也无法保存 String-Builder 变量 imagepathb 的值。

  for(int i = 0; i < al.length; i++){

     imagepathb.append(al[i]).append(",");
     Toast.makeText(getApplicationContext(), "This is image path : "+ imagepathb, Toast.LENGTH_LONG).show();        
     sharedpreferences.edit().putString("imgpath",imagepathb.tostring()).commit();                    

  } 

onActivityResult 收到结果时,我想将此字符串数组存储在共享首选项中。但我不知道它如何在loop 中处理多种语法。 任何人都可以将这个字符串数组存储在我的共享偏好中。提前谢谢你。

【问题讨论】:

  • 第一种语法和第二种语法是什么意思??
  • @Clairvoyant 我的意思是,这是 for 循环中的第一个语法 imagepathb.append(al[i]).append(",");。这是第二个语法 Toast.makeText(getApplicationContext(), "This is image path : "+ imagepathb, Toast.LENGTH_LONG).show(); 当我只运行第一个字符串生成器变量 imagepatb 可以存储 tamperory 的值,但 toast 不能显示 imagepathb 的值
  • @Clairvoyant 当我只执行 System.out.println(imagepathb);Toast with imagepathb 变量输出 toast 消息显示完美。但是当尝试使用共享首选项保存此字符串数组时,它仅显示 toast 并且共享首选项变量保持为空(原样)。
  • sharedpreference 行放在循环之后。
  • @Clairvoyant 我也试过这个,它显示空吐司消息。

标签: android arrays arraylist sharedpreferences onactivityresult


【解决方案1】:

我认为最好的方法是将其保存在数据库中,共享首选项可以很好地保存用户连接的变量(用户,密码,...),初始变量等...但是不保存数组列表,它是不是一个好的编程习惯!我也知道你不能将数组列表保存为数组列表,但如果作为集合,你可以看到这个post

告诉我我是否帮助过你和好的编程!

【讨论】:

  • @peter 你能告诉我如何将数组列表转换为set&lt;string&gt;.?因为我在第一个活动中传递数组列表。
  • 如果您想查看帖子,请点击超链接(帖子字词),如果您想将数组列表从活动传递到另一个活动,那么您需要查找有关捆绑包的文档!
  • @peter 你能帮我吗on this question
【解决方案2】:

首先,如果已知 ArrayList 中的所有字符串都是唯一的(即没有重复),则可以将 ArrayList 转换为 Set 并将其存储,使用putStringSet() 方法:

sharedpreferences.edit().putStringSet("imgpath", new HashSet<String>(a)).commit();

加载时,可以将其转换回ArrayList

ArrayList<String> myList = 
      new ArrayList<>(sharedpreferences.getStringSet("imgpath", new HashSet<String>()));

另一种方法是存储多个具有不同名称的值:

Edit edit = sharedpreferences.edit();
int cnt = 0;
for (String s : a) 
   edit.putString("imgpath_" + (cnt++), s);
while (sharedpreferences.getString("imgpath_" + cnt, null) != null) {
  edit.remove("imgpath_" + cnt); // delete all extra values from previous save time;
  cnt++;
}
edit.commit();

加载时你必须检查直到你得到一个空值;

ArrayList<String> a = new ArrayList<>();
for(int cnt = 0;; cnt++) {
   String s = sharedpreferences.getString("imgpath_" + cnt, null);
   if (s == null) break;
   a.add(s);
}

最后,你可以用逗号分隔:

StringBuilder sb = new StringBuilder();
for (String s : a) {
    is (sb.length() > 0) sb.append(',');
   sb.append(s);
}
sharedpreferences.edit().putString("imgpath",sb.tostring()).commit();                    

加载时,可以使用String.split来取回数组

String[] al = sharedPreferences.getString("imgpath", "").split(',');

【讨论】:

  • @peter 只是您在第一个代码中遗漏的一件事。你不用 commit(); 保存它而没有 commit() 它就像一个简单的变量一样工作。
猜你喜欢
  • 1970-01-01
  • 2012-08-22
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多