当前位置:操作系统 > 安卓/Android >>

Android编程心得-SharedPreference中putStringset存取数据

在我们使用SharedPreference的时候,常用的就是存储配置文件信息,但有时我们需要存储易做图数组信息的时候,就可以用到putStringSet。

下面是写方法:


[html]
public void SaveSiteinfoToXml() { 
    final SharedPreferences prefs = PreferenceManager 
            .getDefaultSharedPreferences(MainActivity.this); 
    Editor editor = prefs.edit(); 
    Set<String> siteno = new HashSet<String>(); 
    if (Unit_PublicVar.arr_DeatilContent != null) { 
        for (int i = 0; i < Unit_PublicVar.arr_DeatilContent.length; i++) { 
            siteno.add(Unit_PublicVar.arr_DeatilContent[i][0] + "," 
                    + Unit_PublicVar.arr_DeatilContent[i][1] + "," 
                    + Unit_PublicVar.arr_DeatilContent[i][2] + "," 
                    + Unit_PublicVar.arr_DeatilContent[i][3]); //  
 
        } 
    } 
    editor.putStringSet("站点信息", siteno); 
 
    editor.commit(); 

 public void SaveSiteinfoToXml() {
  final SharedPreferences prefs = PreferenceManager
    .getDefaultSharedPreferences(MainActivity.this);
  Editor editor = prefs.edit();
  Set<String> siteno = new HashSet<String>();
  if (Unit_PublicVar.arr_DeatilContent != null) {
   for (int i = 0; i < Unit_PublicVar.arr_DeatilContent.length; i++) {
    siteno.add(Unit_PublicVar.arr_DeatilContent[i][0] + ","
      + Unit_PublicVar.arr_DeatilContent[i][1] + ","
      + Unit_PublicVar.arr_DeatilContent[i][2] + ","
      + Unit_PublicVar.arr_DeatilContent[i][3]); //

   }
  }
  editor.putStringSet("站点信息", siteno);

  editor.commit();
 }
putStringset需要传入Set<String> 类型的参数  ,本函数的二维数组之前已经有值了,所以此处直接存储是没有问题的。

下面是读方法


[java]
   public void LoadParaFromXml() { 
        final SharedPreferences prefs = PreferenceManager 
                .getDefaultSharedPreferences(MainActivity.this); 
        Set<String> siteno = new HashSet<String>(); 
        siteno = prefs.getStringSet("站点信息", siteno); 
        if (siteno.size() > 0) {      
            String[] data = (String[]) siteno.toArray(new String[siteno.size()]);   //将SET转换为数组   
            Unit_PublicVar.arr_DeatilContent = new String[data.length][]; 
            for (int i = 0; i < data.length; i++) { 
                Unit_PublicVar.arr_DeatilContent[i] = data[i].trim().split( 
                        ","); 
            } 

 public void LoadParaFromXml() {
  final SharedPreferences prefs = PreferenceManager
    .getDefaultSharedPreferences(MainActivity.this);
  Set<String> siteno = new HashSet<String>();
  siteno = prefs.getStringSet("站点信息", siteno);
  if (siteno.size() > 0) {  
   String[] data = (String[]) siteno.toArray(new String[siteno.size()]); //将SET转换为数组 
   Unit_PublicVar.arr_DeatilContent = new String[data.length][];
   for (int i = 0; i < data.length; i++) {
    Unit_PublicVar.arr_DeatilContent[i] = data[i].trim().split(
      ",");
   }
}
}

 

 

 

 


 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,