2011-07-14 16 views
18

He creado una actividad en la que he usado preferencias compartidas para almacenar datos ... ahora en otra actividad tengo un botón de reinicio ... cuando hago clic en el botón de reinicio, el data store se lost..so cómo puede ser done..my código escómo restablecer todos los almacenes de datos almacenados usando preferencias compartidas

código en actividad1:

public void writeToRegister() 
    { 

      // Write history data to register 
      SharedPreferences preferences1 = getPreferences(MODE_PRIVATE); 
      SharedPreferences.Editor editor1 = preferences1.edit(); 
      editor1.putInt("iHistcount", CycleManager.getSingletonObject().iHistCount); 
      for(int i=0;i< CycleManager.getSingletonObject().iHistCount;i++) 
      { 
       editor1.putLong("dtHistoryDate"+Integer.toString(i), CycleManager.getSingletonObject().dtHistory[i].getTime()); 

      } 
      editor1.commit(); 
    } 

    public void readFromRegister() 
    { 
      // Read history data from register 
      SharedPreferences preferences1 = getPreferences(MODE_PRIVATE); 
      CycleManager.getSingletonObject().iHistCount=preferences1.getInt("iHistcount", 0); 
      for(int i=0;i< CycleManager.getSingletonObject().iHistCount;i++) 
      { 
       Long x=preferences1.getLong("dtHistoryDate"+Integer.toString(i), 0L); 
       CycleManager.getSingletonObject().dtHistory[i]=new Date(x); 
      } 
    } 

código para la Actividad 2:

Button pBtnReset = new Button(this); 
    pBtnNextMonth.setOnClickListener(pBtnReset OnClickListener); 
    Button.OnClickListener pBtnReset OnClickListenernew Button.OnClickListener() 
    { 
       public void onClick(View arg0) 
       { 


       } 
    }; 

así que lo que tengo que escribir en s botón de restablecimiento de actividad econd para que borre los datos almacenados

Respuesta

48

Consigue tu Editor y llame clear() algo como esto: Editar: como el usuario DDoSAttack mentio ned. Hay dos maneras de conseguir SharedPreferences

1: Llegar por defecto SharedPreferences

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con); 

2: conseguir específica SharedPreferences

SharedPreferences prefs = Context.getSharedPreferences("FileName", Context.MODE_PRIVATE); 

y aquí es cómo va a desactivarla.

public void clear() 
{ 
    SharedPreferences prefs; // here you get your prefrences by either of two methods 
    Editor editor = prefs.edit(); 
    editor.clear(); 
    editor.commit(); 
} 
+0

no, no funciona \ – AndroidDev

+0

ver mi respuesta editada. –

+0

@AdilSoomro ¿Qué es la variable con 'getDefaultSharedPreferences (con);'? ¿Es contexto? – NPE

3

Utilice SharedPreferences.Editor clear() método.

Ver Documentation

SharedPreferences preferences = getPreferences(0); 
     SharedPreferences.Editor editor = preferences.edit(); 

     editor.clear(); 
     editor.commit(); 
+0

En mi código ¿Cómo puedo llamarlo – AndroidDev

+0

poner este código, ya que es en su método onClick. – Marmoy

4

Si desea borrar todos los datos en una llamada archivo de preferencias clear() del SharedPreferences.Editor ejemplo

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear()

+2

Además, no creo que su código funcione como está escrito. Debe especificar un nombre de archivo para la clase SharedPreferences. Por lo tanto, debería verse así: SharedPreferences preferences1 = getSharedPreferences ("file_name", MODE_PRIVATE); – rf43

4

es muy fácil ..

yourEditor.remove(" thing you want to remove on start"); 

y luego dar obligada

yourEditor.commit(); 
Cuestiones relacionadas