Domanda

I have a class in which I have an option button that opens a dialog containing 6 checkboxes. By default, 4 checkboxes are set to true when the app is first loaded. The user can check and uncheck these checkboxes. When I press the backbutton, the dialog disappears and if again click the "option" button the checkboxes are not checked/unchecked as per the last state selected. I have used shared preferences to hold these values.

My Java class code is here. I have removed the unnecessary methods below and also posted the xml layout. Please help as to why the shared preferences is not working.

GlamDokuActivity

public class GlamDokuActivity extends Activity{

    private Button easy,fair,hard,evil,insane;
    private CheckBox screenOn,clashSquares,quickNotes,soundOn,showTimer,matchingNumbers;
    SharedPreferences sharedpreferences;
    SharedPreferences.Editor editor;
    public static final String DefaultChoicesOn = "defaults";
    public static final String Timer ="true";
    public static final String QuickNotes ="true";
    public static final String ScreenOn ="false";
    public static final String ClashingSquares ="true";
    public static final String SoundOn ="false";
    public static final String MatchingNumbers ="true";

    //keeping a tab on the status of the above default options
    private boolean timerStatus=false,quickNotesStatus=false,screenOnOffStatus=false,clashingSquaresStatus=false,soundOnOffStatus=false,matchingNumberStatus=false;


       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.glamdoku_main);

          //call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
          sharedpreferences=getSharedPreferences(GlamDokuActivity.DefaultChoicesOn, Context.MODE_PRIVATE);

          //saving in the shared preferences
          editor = sharedpreferences.edit();

          //If the key is not present then only add the key-value pair
          if(!sharedpreferences.contains(Timer))editor.putBoolean(GlamDokuActivity.Timer, true);
          if(!sharedpreferences.contains(ClashingSquares))editor.putBoolean(GlamDokuActivity.ClashingSquares, true);
          if(!sharedpreferences.contains(MatchingNumbers))editor.putBoolean(GlamDokuActivity.MatchingNumbers, true);
          if(!sharedpreferences.contains(SoundOn))editor.putBoolean(GlamDokuActivity.SoundOn, false);
          if(!sharedpreferences.contains(ScreenOn))editor.putBoolean(GlamDokuActivity.ScreenOn, false);
          if(!sharedpreferences.contains(QuickNotes))editor.putBoolean(GlamDokuActivity.QuickNotes, true);
          editor.commit();

          //KEEP THE SCREEN ON
          if(sharedpreferences.getBoolean(ScreenOn, true))getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


         //System.out.println("I AM IN ONCREATE OF ");

       }//onCreate() ends


        public void optionsMethod(View v){//options button click method 

            //dialog that appears when the hints button is clicked
            final Dialog dialog = new Dialog(GlamDokuActivity.this,R.style.dialogStyle);

            dialog.setContentView(R.layout.layout_options);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.setCanceledOnTouchOutside(true);
            dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
            dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            dialog.show();

            //tagging the ids
            screenOn = (CheckBox)dialog.findViewById(R.id.screenOn);
            clashSquares = (CheckBox)dialog.findViewById(R.id.clash);
            quickNotes = (CheckBox)dialog.findViewById(R.id.quickNotes);
            soundOn  = (CheckBox)dialog.findViewById(R.id.soundOnOff);
            showTimer = (CheckBox)dialog.findViewById(R.id.timer);
            matchingNumbers = (CheckBox)dialog.findViewById(R.id.matchNumbers);

            //default values from shared preferences --- depending upon these the state of the respective checkboxes to change
            timerStatus=sharedpreferences.getBoolean(GlamDokuActivity.Timer, true);
            quickNotesStatus=sharedpreferences.getBoolean(GlamDokuActivity.QuickNotes, true);
            screenOnOffStatus=sharedpreferences.getBoolean(GlamDokuActivity.ScreenOn, false);
            clashingSquaresStatus=sharedpreferences.getBoolean(GlamDokuActivity.ClashingSquares, true);
            soundOnOffStatus=sharedpreferences.getBoolean(GlamDokuActivity.SoundOn, false);
            matchingNumberStatus=sharedpreferences.getBoolean(GlamDokuActivity.MatchingNumbers, true);



            //check or uncheck depending upon the aforementioned boolean values
            if(timerStatus==false)showTimer.setChecked(false); else showTimer.setChecked(true);
            if(quickNotesStatus==false)quickNotes.setChecked(false);else quickNotes.setChecked(true);
            if(screenOnOffStatus==false)screenOn.setChecked(false);else screenOn.setChecked(true);
            if(clashingSquaresStatus==false)clashSquares.setChecked(false);else clashSquares.setChecked(true);
            if(soundOnOffStatus==false)soundOn.setChecked(false);else soundOn.setChecked(true);
            if(matchingNumberStatus==false)matchingNumbers.setChecked(false);else matchingNumbers.setChecked(true);

            //-----------------------------------------------------------------------------------------------------------------
            //click the screenOn chkbox
            screenOn.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,1));

            //click the clashing squares chkbox
            clashSquares.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,2));

            //click the quick notes chkbox
            quickNotes.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,3));

            //click the sound chkbox
            soundOn.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,4));

            //click the show timer chkbox
            showTimer.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,5));

            //click the matching numbers chkbox
            matchingNumbers.setOnClickListener(new CommonCheckClickListener(dialog,GlamDokuActivity.this,6));



@Override       
public void onResume(){

    //KEEP THE SCREEN ON
    if(sharedpreferences.getBoolean(ScreenOn, true))getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    super.onResume();
}

}

CommonCheckClickListener

public class CommonCheckClickListener implements OnClickListener {

    private Dialog dialog;
    private SherlockActivity activity;
    private int thisOption;
    SharedPreferences sharedpreferences;
    SharedPreferences.Editor editor;

public CommonCheckClickListener(Dialog dialog, Activity activity, int thisOption){
    this.activity=activity;
    this.dialog=dialog;
    this.thisOption=thisOption;

      //call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
      sharedpreferences=activity.getSharedPreferences(GlamDokuActivity.DefaultChoicesOn, Context.MODE_PRIVATE);
      editor=sharedpreferences.edit();
}

@Override
public void onClick(View v) {

    int flag=0;

     if(((CheckBox)v).isChecked())flag=1; else flag=0;//flagged as checked


     switch(thisOption){

     case 1: //screenOn option

         if(flag==1){
             editor.putBoolean(GlamDokuActivity.ScreenOn, true);
             Toast.makeText(activity, "Warning - Keeping the screen always on consumes battery.", Toast.LENGTH_LONG).show();
            }
                else{
                    editor.putBoolean(GlamDokuActivity.ScreenOn, false);
                }
             editor.commit();

             boolean screenOn = sharedpreferences.getBoolean(GlamDokuActivity.ScreenOn, true);


     break;

     case 2: //clashing squares option

         if(flag==1)editor.putBoolean(GlamDokuActivity.ClashingSquares, true);else editor.putBoolean(GlamDokuActivity.ClashingSquares, false);
         editor.commit();

         boolean cs = sharedpreferences.getBoolean(GlamDokuActivity.ClashingSquares, true);


     break;

     case 3: //quick notes option

         if(flag==1)editor.putBoolean(GlamDokuActivity.QuickNotes, true); else editor.putBoolean(GlamDokuActivity.QuickNotes, false);
         editor.commit();

         boolean qn = sharedpreferences.getBoolean(GlamDokuActivity.QuickNotes, true);


     break;

     case 4: //sound option

         if(flag==1)editor.putBoolean(GlamDokuActivity.SoundOn, true); else editor.putBoolean(GlamDokuActivity.SoundOn, false);
         editor.commit();

         boolean sound = sharedpreferences.getBoolean(GlamDokuActivity.SoundOn, true);


     break;

     case 5: //show Timer option

         if(flag==1)editor.putBoolean(GlamDokuActivity.Timer, true); else editor.putBoolean(GlamDokuActivity.Timer, false);
         editor.commit();

         boolean timer = sharedpreferences.getBoolean(GlamDokuActivity.Timer, true);

     break;

     case 6: //matching numbers option

         if(flag==1)editor.putBoolean(GlamDokuActivity.MatchingNumbers, true); else editor.putBoolean(GlamDokuActivity.MatchingNumbers, false);
         editor.commit();

         boolean matching = sharedpreferences.getBoolean(GlamDokuActivity.MatchingNumbers, true);

     break;
     }
}

Main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:gravity="center"
        >


        <Button
             style="@style/button_main_screen"
             android:id="@+id/playsudoku"
             android:onClick="playMethod"
             android:text="@string/playsudoku" />

             <Button
             style="@style/button_main_screen"
             android:id="@+id/options"
             android:onClick="optionsMethod"
             android:text="@string/optionssudoku" />
       </LinearLayout>

Dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_vertical" >

                      <CheckBox
                          android:id="@+id/matchNumbers"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:checked="true"

                          android:layout_marginTop="21dp"
                          android:text="@string/match_numbers" />

                      <CheckBox
                          android:id="@+id/clash"
                          android:checked="true"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="@string/clash_squares" />



                      <CheckBox
                          android:checked="true"
                          android:id="@+id/quickNotes"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"

                          android:text="@string/quick_notes" />

                      <CheckBox
                          android:id="@+id/timer"
                          android:checked="true"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="@string/timer_always" />

                      <CheckBox
                          android:id="@+id/screenOn"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginTop="22dp"
                          android:text="@string/screen_always" />

                      <CheckBox
                          android:id="@+id/soundOnOff"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginLeft="14dp"
                          android:layout_marginTop="25dp"
                          android:text="@string/sound_off" />
    </LinearLayout>
È stato utile?

Soluzione

The definition of the static key-variables is incorrect. They need to be other names rather than "true" and "false". The problem is solved once you change the initialization values. – Ganesh Hegde

Altri suggerimenti

You should use OnCheckedChangeListener instead of OnClickListener for your CheckBoxes. OnClickListener#onClick calls earlier then CheckBox's checked state changed.

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