2016-09-22 35 views
0

これは初心者です。これは初めてのアプリで、トグルスイッチの状態を保存するのに問題があります。アプリを終了すると、トグルの状態がクリアされ、トグルされていないかのように見えます。私はすべての解決策を見てきましたが、私がエラーとクラッシュを得る方法を実装しようとすると、私が見たすべての例は、私のコードと比較してシンプルに見えるので、私はそれを行う方法を理解できません。誰もが私は本当に幸せになるソリューションに私を指すことができればToggleスイッチ/チェックボックスの状態をSharedPreferencesで保存する

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

は以前にこれらのスレッドをチェックアウトしました。

@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); 
        } 
       } 
      } 
     }); 

    } 

わかりましたので、私は私が間違って何をやっていた...私はの状態を保存するために「SharedPreferences.Editor」のペアを追加しました私の以前の試みで考え出したと思う:ここに関連するコードの抜粋ですif/elseステートメントが互いにネストされているので、間違ったif/elseステートメントで切り替えます。以下は実装です

@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

「スイッチ」はいくつありますか? 1つまたは複数の?? – tpk

+0

2つのスイッチがあります。 – UberNoob

+0

最高のハッピーコーディング。 – tpk

答えて

1

Sharedpreferencesは1つのストレージオプションです。

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

をそして、あなたはsharedpreferencesを使用してより多くの情報が必要な場合はストレージとしてここを見て:書き込み、sharedpreferencesの情報を取得するには、この答えはあなたを助ける

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

()メソッドを使用して、Switchの過去の状態をSharedpreferencesから取得したい場合は(そこにスイッチの状態を書き込んだと仮定します)。設定から状態を取得したら、OnCreate内でスイッチのsetChecked(boolean checked)メソッドを使用してボタンのチェック状態を設定する必要があります。ここにドキュメント:https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)

あなたのsetOnCheckedListenerでは、OnCheckedListenerが呼び出されるたびにSharedPreferencesに書き込むことにします。この方法で、OnCreate()メソッドのSharedpreferencesからこのチェック済み/未チェックの状態を取得できます。

+0

助けてくれてありがとう。 – UberNoob

関連する問題