Próbuję wykonać sprawdzanie i chcę zapisać wartość w SharedPreferences. I'am ale nie jestem pewien, czy to działaSharedPreferences Wyczyść/zapisz
To co mam zrobić, aby zapisać wartość wynosi: *
SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
boolean firstrun = prefs.getBoolean("firstrun", true);
db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory
if (firstrun) {
SharedPreferences.Editor editor = prefs.edit();
db.execSQL("CREATE TABLE startValue (ID Integer Primary Key, myValue Integer)");
db.execSQL("INSERT INTO startValue (myValue) VALUES (2)");
editor.putBoolean("firstrun", false);
editor.apply();
}
// Save the state
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("firstrun", false)
.commit();
I Wyczyść preferenece z innej działalności jest:
try{
db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory
db.execSQL("DROP TABLE IF EXISTS startValue");
db.close();
SharedPreferences preferences = getPreferences(0);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("firstrun");
editor.clear();
editor.commit();
this.finish();
}
catch(SQLException ex)
{
//catch error here
}
Numer
Ale kiedy jestem testowanie, ponieważ widzę, że nie usuwa preferencji. Czy robię coś źle, czy?
SharedPreferences preferencje = getSharedPreferences ("preferencji", 0); czy powinienem zmienić "PREFERENCJĘ" na "firstrun" lub? – Tirolel
Nie.Ten kod usunie wszystkie SharedPreferences. – AwadKab
To jest pomocne, ale jak wyczyścić część sharedPreferences? – alicanbatur