2 回答

TA貢獻1824條經驗 獲得超5個贊
對于將值設置為共享首選項
SharedPreferences.Editor editor = getSharedPreferences("ProgressBarData",
MODE_PRIVATE).edit();
editor.putInt("progress", 15);
editor.apply();
從共享偏好中獲取價值
SharedPreferences prefs = getSharedPreferences(ProgressBarData,
MODE_PRIVATE);
int progress = prefs.getInt("progress", 0);

TA貢獻1829條經驗 獲得超7個贊
第一個你知道你的錯誤?你不能將你的數(shù)據(jù)存儲在局部變量中,因為在活動結束時它破壞了所有東西,當你回到android活動時它將再次啟動所有東西并且所有東西都會重新啟動
您會有更好的理解
現(xiàn)在你的解決方案
如果你想每天存儲數(shù)據(jù)和處理,最好使用本地存儲,如 Sqlite、room 或共享首選項。
做任務的步驟
將數(shù)據(jù)存儲、獲取和刪除到共享首選項需要三個步驟
用于存儲、獲取、刪除數(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");
添加回答
舉報