2013-03-18 16 views
15

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?

Odpowiedz

60

Aby wyczyścić SharedPreferences użyć tej

SharedPreferences preferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = preferences.edit(); 
editor.clear(); 
editor.commit(); 

nadzieję, że to pomogło ci.

+0

SharedPreferences preferencje = getSharedPreferences ("preferencji", 0); czy powinienem zmienić "PREFERENCJĘ" na "firstrun" lub? – Tirolel

+2

Nie.Ten kod usunie wszystkie SharedPreferences. – AwadKab

+0

To jest pomocne, ale jak wyczyścić część sharedPreferences? – alicanbatur

6

Nie używasz tych samych preferencji. Potrwać do zapoznania http://developer.android.com/reference/android/app/Activity.html

w pierwszym aktywności używasz:

SharedPreferences prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE); 

A w pozostałej działalności rozliczeń używasz tylko:

SharedPreferences preferences = getPreferences(0); 

Czytając dokumenty:

Pobierz obiekt SharedPreferences, aby uzyskać dostęp do preferencji, które są prywatne dla tego działania. To po prostu wywołuje bazową metodę getSharedPreferences (String, int), przekazując nazwę klasy tego działania jako nazwę preferencji.

Musisz użyć tej samej nazwy preferencji w obu działaniach. Więc w drugim aktywności, gdzie można zrobić polanę wystarczy użyć

SharedPreferences preferences = getSharedPreferences("PREFERENCE", MODE_PRIVATE); 
+0

to wyjaśnienie, którego szukałem bardzo dziękuję, ale rozwiązanie AwadKab działało dla mnie. Ale z twoim wyjaśnieniem rozumiem kod AwaKabs. Dziękuję – Tirolel

+1

Nie ma problemu, cieszę się, że to załatwiłeś. –

3

// zapisać ciąg SharedPreferences

public static void saveStringToSharedPreferences(Context mContext, String key, String value) { 
    if(mContext != null) { 
     SharedPreferences mSharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0); 
     if(mSharedPreferences != null) 
      mSharedPreferences.edit().putString(key, value).commit(); 
    } 
} 

// Odczyt ciąg z SharedPreferences

public static String readStringFromSharedPreferences(Context mContext, String key) { 
    if(mContext != null) { 
     SharedPreferences mSharedPreferences = mContext.getSharedPreferences("SHARED_PREFERENCES", 0); 
     if(mSharedPreferences != null) 
      return mSharedPreferences.getString(key, null); 
    } 
    return null; 
} 

// usuń z SharedPreferences

public static void removeFromSharedPreferences(Context mContext, String key) { 
    if (mContext != null) { 
     SharedPreferences mSharedPreferences = mContext.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, 0); 
     if (mSharedPreferences != null) 
      mSharedPreferences.edit().remove(key).commit(); 
    } 
} 
1

Wystarczy można:

getSharedPreferences("PREFERENCE", 0).edit().clear().commit(); 
0

usuwając wszystkie preferencje:

SharedPreferences mPrefs_delete = context.getSharedPreferences(GeneralFunctions.SETTING_SINGLE_MASTER_CHILD, Context.MODE_PRIVATE); 
haredPreferences.Editor editor_delete = mPrefs_delete.edit(); 
editor_delete.clear().commit(); 
Powiązane problemy