保存:
public static boolean saveArray() { SharedPrefernces sp=SharedPrefernces.getDefaultSharedPrefernces(this); SharedPrefernces.Editor mEdit1= sp.edit(); mEdit1.putInt("Status_size",sKey.size()); /*sKey is an array*/ for(int i=0;i<sKey.size();i++) { mEdit1.remove("Status_" + i); mEdit1.putString("Status_" + i, sKey.get(i)); } return mEdit1.commit(); }
取值:
public static void loadArray(Context mContext) { Shared Preferences mSharedPreference1=PreferenceManager.getDefaultSharedPreferences(mContext); sKey.clear(); int size = mSharedPreference1.getInt("Status_size", 0); for(int i=0;i<size;i++) { sKey.add(mSharedPreference1.getString("Status_" + i, null)); } }
android中保存一个ArrayList到SharedPreferences的方法
时间: 2024-10-05 07:56:09