SharedPreferences 儲存字串陣列

2021-06-29 06:10:26 字數 586 閱讀 4054

1.android3.0之後,sharedpreferences提供了putstringset方法,可以儲存字串陣列,例子:

sharedpreferences sp = mcontext.getsharedpreferences("data", context.mode_private);

editor et = sp.edit();

set set = new hashset();

set.add("hello");

et.putstringset(key, set);

2.但需要注意的是,set中不允許重複元素,若有重複元素,它會自動刪除。不滿足我的需求,所以這裡採取這樣的方式存放字串陣列:

構建兩個函式,用於讀取和存放 字串陣列

public string getsharedpreference(string key)

public void setsharedpreference(string key, string values)

editor et = sp.edit();

et.putstring(key, str);

et.commit();} }

SharedPreferences儲存資料

sharedprefrences是android中最容易理解的資料儲存技術,實際上sharedprefrences處理的就是乙個key value 鍵值對 sharedprefrences常用來成儲存一些輕量級的資料。使用方法 儲存資料 1 例項化sharedprefrences物件 2 例項化sh...

SharedPreferences儲存資料工具類

import android.content.context import android.content.sharedpreferences import android.content.sharedpreferences.editor public class preferenceutils r...

SharedPreferences儲存資料

適用範圍 儲存少量的資料,且這些資料的格式非常簡單 字串型 基本型別的值。比如應用程式的各種配置資訊 如是否開啟音效 是否使用震動效果 小遊戲的玩家積分等 解鎖口 令密碼等 context.mode private context.mode world readable context.mode w...