zoukankan      html  css  js  c++  java
  • android中保存一个ArrayList到SharedPreferences的方法

    保存:

    1. public static boolean saveArray() {  
    2.      SharedPrefernces sp=SharedPrefernces.getDefaultSharedPrefernces(this);  
    3.      SharedPrefernces.Editor mEdit1= sp.edit();  
    4.      mEdit1.putInt("Status_size",sKey.size()); /*sKey is an array*/   
    5.   
    6.     for(int i=0;i<sKey.size();i++) {  
    7.         mEdit1.remove("Status_" + i);  
    8.         mEdit1.putString("Status_" + i, sKey.get(i));    
    9.     }  
    10.   
    11.     return mEdit1.commit();       
    12. }  


    取值:

     
    1. public static void loadArray(Context mContext) {    
    2.     Shared Preferences mSharedPreference1=PreferenceManager.getDefaultSharedPreferences(mContext);  
    3.     sKey.clear();  
    4.     int size = mSharedPreference1.getInt("Status_size"0);    
    5.   
    6.     for(int i=0;i<size;i++) {  
    7.         sKey.add(mSharedPreference1.getString("Status_" + i, null));    
    8.   
    9.     }  
    10. }  


    在API 11以后,SharedPreferences添加了对Sets的支持,我们可以将List转化为HashSet,然后直接存储。

  • 相关阅读:
    植物:吊兰
    植物:文竹
    理论:混沌理论
    图书-数学:《数学之美》
    调味品:味精
    linux查看文件大小df-du
    linux防火墙开启-关闭
    tomcat端口号、日志、启停
    chmod
    jdk-tomcat环境变量设置
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3696303.html
Copyright © 2011-2022 走看看