Android編程心得-SharedPreference中putStringset存取資料

來源:互聯網
上載者:User

在我們使用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(
      ",");
   }
}
}

 

 

 

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.