Domanda

Così ho questa attività e ho 2 caselle di controllo, quando l'utente fa clic su uno di essi, verrà controllato e salvato ma se chi chiude l'app e riapri le caselle di controllo non vengono salvate, che potrebbe essere il problema?Non è salvato, o non è caricato?

Ecco il mio codice

package com.myappisawesome;

import com.myappisawesome.R;
import com.pushbots.push.Pushbots;

import android.R.string;
import android.app.Activity;
import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class settup extends Activity {
     private String SENDER_ID = "ccccc";
     private String PUSHBOT_ID = "ccccc";
     public static final String PREFS_NAME = "BuyMeCheckBoxes";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notifsettings);
        Pushbots.init(this, SENDER_ID , PUSHBOT_ID);
        Pushbots.getInstance().setMsgReceiver(GoToApp.class);
        Pushbots.getInstance().setRegStatus(true);


         final CheckBox auto = (CheckBox) findViewById(R.id.checkBox1);
         final CheckBox imobiliare = (CheckBox) findViewById(R.id.checkBox2);
          SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean c1 = settings.getBoolean("auto", false);
        boolean c2 = settings.getBoolean("imobiliare", false);

        auto.setChecked(c1);
        imobiliare.setChecked(c2);




           if (auto.isChecked()) {
               auto.setChecked(false);
               Pushbots.getInstance().tag(getApplicationContext(), "auto", null);

           }




           if (imobiliare.isChecked()) {
               imobiliare.setChecked(false);
               Pushbots.getInstance().tag(getApplicationContext(), "imobiliare", null);

           }



    }




    @Override
    protected void onStop(){
       super.onStop();
       final CheckBox auto = (CheckBox) findViewById(R.id.checkBox1);
       final CheckBox imobiliare = (CheckBox) findViewById(R.id.checkBox2);

      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();

      boolean checkBoxValue1 = auto.isChecked();
      boolean checkBoxValue2 = imobiliare.isChecked();  

      editor.putBoolean("auto", checkBoxValue1);
      editor.putBoolean("imobiliare", checkBoxValue2);
      editor.commit();;
    }


}
.

È stato utile?

Soluzione

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    auto.setChecked(settings.getBoolean("auto", false));
    auto.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean("auto", auto.isChecked());
                editor.commit();;
            }
        });
.

Utilizzare SetonCheckedChangelistener nel tuo ONCreate.E salva le tue preferenze lì invece di Onstop.Non è una buona pratica salvare le tue preferenze in Onstop.

Altri suggerimenti

Sembra che tu stia impostando lo stato della casella di controllo e quindi sovrascriverlo in seguito nella tua attività. Fai auto.setChecked(c1);

Quindi più tardi si esegue if (auto.isChecked()) { auto.setChecked(false); }

È necessario utilizzare il listener OnCheckCoxClicker (View View) per ottenere l'ingresso dell'utente e salvarlo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top