mardi 29 septembre 2015

android checkbox is checked always true

I define a checkbox and assign no value to it. every time I clicked on it and print the value of checkbox, It shows true all The time. I put the result in preferences and then retrieve it. why is it always true?

public class CallAndSms extends Activity{
static SQLiteDatabase myDB= null;
static     Context context ;
static ListView lv;
static CallAndSmsAdaptor adapter;
static ArrayList<String> AllData= new ArrayList<String>();
CheckBox save;
static SharedPreferences preferences;
SharedPreferences.Editor editor=null;
 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);





    setContentView(R.layout.sms_call);
     preferences = getSharedPreferences("modes",Context.MODE_PRIVATE);
     editor = preferences.edit();



    save= (CheckBox) findViewById(R.id.save);


    save.setChecked(preferences.getBoolean("save", true));



    context=this;


     save.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

            if ( save.isChecked()){
                Log.i("save save", save.isChecked()+"");

                  editor.putBoolean("save", true);

            }

            else 

                  {editor.putBoolean("save", false);}

            Log.i("pref", preferences.getBoolean("save", true)+"");


        }
    });


 }
 }




Aucun commentaire:

Enregistrer un commentaire