0

Donc, je suis un débutant c'est ma première application et j'ai du mal à sauvegarder l'état des commutateurs à bascule. Lorsque je quitte l'application, il efface l'état de la bascule et fait ressembler si n'a pas été basculé. J'ai cherché partout la solution mais quand j'essaye de mettre en application comment son suggéré j'obtiens des erreurs et des accidents. Tous les exemples que j'ai vus semblent simples en comparaison avec mon code, donc je ne peux pas comprendre comment le faire.Enregistrer l'état du commutateur bascule/case à cocher avec SharedPreferences

ces fils Contrôlé précédemment:

Save SWITCH button state, and recover state with SharedPrefs

Sharedpreferences with switch button

How to save the state of the toogleButton on/off selection?

how to save the state of switch(button) in android

Si quelqu'un pouvait me pointer vers une solution que je serais vraiment heureux. Voici un extrait du code correspondant:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     switchButton_Stat = (Switch) findViewById(switch_s); 
     textView_S = (TextView) findViewById(R.id.textView_S); 
     textView_S.setText(switchOff); 

     switchButton_Stat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) { 
       if (bChecked) { 
        textView_S.setText(switchOn); 
        CompoundButton toggle_d = (CompoundButton)findViewById(R.id.switch_d); 
        if (toggle_d.isChecked()){ 
         NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
         NM.cancelAll(); 
         handler_mon.removeCallbacks(runnable_mon); 
         handler_sun.postDelayed(runnable_sun,300); 
        } 
        else { 
         NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
         NM.cancelAll(); 
         handler_sun.removeCallbacks(runnable_sun); 
         handler_mon.postDelayed(runnable_mon,300); 
        } 
       } 
       else { 
        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        NM.cancelAll(); 
        handler_mon.removeCallbacks(runnable_mon); 
        handler_sun.removeCallbacks(runnable_sun); 
        textView_S.setText(switchOff); 
       } 
      } 
     }); 


     switchButton_Day = (Switch) findViewById(R.id.switch_d); 
     textView_D = (TextView) findViewById(R.id.textView_D); 
     textView_D.setText(MonOff); 

     switchButton_Day.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean dChecked) { 
       if (dChecked) { 
        textView_D.setText(SunOff); 
        Switch switch_s = (Switch) findViewById(R.id.switch_s); 
        if (switch_s.isChecked()){ 

         NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
         NM.cancelAll(); 
         handler_mon.removeCallbacks(runnable_mon); 
         handler_sun.postDelayed(runnable_sun,300); 
        } 

        Log.i(TAG, "Day Switch"); 
       } else { 
        textView_D.setText(MonOff); 
        Switch switch_s = (Switch) findViewById(R.id.switch_s); 
        if (switch_s.isChecked()){ 

         NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
         NM.cancelAll(); 
         //NM.cancel(2); 
         handler_sun.removeCallbacks(runnable_sun); 
         handler_mon.postDelayed(runnable_mon,300); 
        } 
       } 
      } 
     }); 

    } 

Ok, donc je pense que je me suis dit ce que je faisais mal ... Dans mes tentatives précédentes j'ai ajouté le « SharedPreferences.Editor » paire d'enregistrer l'état de la bascule dans la mauvaise instruction if/else puisque j'ai les instructions if/else imbriquées les unes dans les autres. Ci-dessous la mise en œuvre

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    switchButton_Stat = (Switch) findViewById(switch_s); 

    SharedPreferences tog_prefs = getSharedPreferences("TOG_PREF", MODE_PRIVATE); 
    boolean tglpref_1 = tog_prefs.getBoolean("tglpref_1", false); 
    if (tglpref_1) { 
     switchButton_Stat.setChecked(true); 
    } else { 
     switchButton_Stat.setChecked(false); 
    } 


    switchButton_Stat = (Switch) findViewById(switch_s); 
    textView_S = (TextView) findViewById(R.id.textView_S); 
    textView_S.setText(switchOff); 

    switchButton_Stat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) { 
      if (bChecked) { 
       SharedPreferences.Editor editor = getSharedPreferences("TOG_PREF", MODE_PRIVATE).edit(); 
       editor.putBoolean("tglpref_1", true); // value to store 
       editor.commit(); 

       textView_S.setText(switchOn); 
       CompoundButton toggle_d = (CompoundButton)findViewById(R.id.switch_d); 
       if (toggle_d.isChecked()){ 


        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        NM.cancelAll(); 
        handler_mon.removeCallbacks(runnable_mon); 
        handler_sun.postDelayed(runnable_sun,300); 
       } 
       else { 
        NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        NM.cancelAll(); 
        handler_sun.removeCallbacks(runnable_sun); 
        handler_mon.postDelayed(runnable_mon,300); 
       } 
      } 
      else { 
       SharedPreferences.Editor editor = getSharedPreferences("TOG_PREF", MODE_PRIVATE).edit(); 
       editor.putBoolean("tglpref_1", false); // value to store 
       editor.commit(); 

       NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
       NM.cancelAll(); 
       handler_mon.removeCallbacks(runnable_mon); 
       handler_sun.removeCallbacks(runnable_sun); 
       textView_S.setText(switchOff); 
      } 
     } 
    }); 
+0

hey combien de Switch (s) y a-t-il? un ou plusieurs? – tpk

+0

Il y a 2 commutateurs – UberNoob

+0

Tout le meilleur et heureux codage. – tpk

Répondre

1

Sharedpreferences est une option de stockage. Pour écrire et récupérer des informations dans sharedpreferences cette réponse vous aidera:

https://stackoverflow.com/a/23024962/6388980

Et si vous voulez plus d'informations sur l'utilisation sharedpreferences que le stockage regardez ici:

https://developer.android.com/guide/topics/data/data-storage.html#pref

Dans le onCreate() méthode que vous souhaitez récupérer l'état passé du commutateur à partir des préférences partagées s'il existe (en supposant que vous avez écrit l'état du commutateur là-bas). Une fois que vous avez récupéré l'état des préférences, vous devez utiliser la méthode setChecked (boolean checked) de Switch dans OnCreate pour définir l'état vérifié du bouton. Documentation ici: https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)

Maintenant dans votre setOnCheckedListener vous voulez écrire dans Sharedpreferences à chaque appel de OnCheckedListener. De cette façon, vous pouvez récupérer cet état coché/non coché à partir de Sharedpreferences dans votre méthode OnCreate().

+0

Je l'ai compris, merci pour l'aide. – UberNoob