2 回答
TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
對(duì)于將值設(shè)置為共享首選項(xiàng)
SharedPreferences.Editor editor = getSharedPreferences("ProgressBarData",
MODE_PRIVATE).edit();
editor.putInt("progress", 15);
editor.apply();
從共享偏好中獲取價(jià)值
SharedPreferences prefs = getSharedPreferences(ProgressBarData,
MODE_PRIVATE);
int progress = prefs.getInt("progress", 0);
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
第一個(gè)你知道你的錯(cuò)誤?你不能將你的數(shù)據(jù)存儲(chǔ)在局部變量中,因?yàn)樵诨顒?dòng)結(jié)束時(shí)它破壞了所有東西,當(dāng)你回到android活動(dòng)時(shí)它將再次啟動(dòng)所有東西并且所有東西都會(huì)重新啟動(dòng)
您會(huì)有更好的理解
現(xiàn)在你的解決方案
如果你想每天存儲(chǔ)數(shù)據(jù)和處理,最好使用本地存儲(chǔ),如 Sqlite、room 或共享首選項(xiàng)。
做任務(wù)的步驟
將數(shù)據(jù)存儲(chǔ)、獲取和刪除到共享首選項(xiàng)需要三個(gè)步驟
用于存儲(chǔ)、獲取、刪除數(shù)據(jù)
//storing
SharedPreferences.Editor editor = context.getSharedPreferences(name,Context.MODE_PRIVATE).edit();
? ? ? ? editor.putString(key, data);
? ? ? ? editor.apply();
//getting
SharedPreferences getSharedPrefrence = context.getSharedPreferences(name, Context.MODE_PRIVATE);
? ? ? ? int data = getSharedPrefrence.getInt(key, IntegerValuesAndStringValues.REGISTER_BEFORE_LOGIN);
? ? ? ? return data;
BasicFunctions.removeSharedPrefrences(getContext(),"Name of the preference");
添加回答
舉報(bào)
