0

Hey guys I want to make my checkbox stay in the same state every time I open my app.. I get this with the 'ja/nein' string, the string states when i close and open again my application... but my checkbox.setchecked(true/false) doesnt work.. please help

public void changeVisitStatus(){
    SharedPreferences visitStatus = mData.getVisitStatus();
    SharedPreferences.Editor editor = visitStatus.edit();

    if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
        editor.putString(mData.mVisitKey, "ja");
        editor.commit();
        mGUI.mBtnVisit.setChecked(true);
    }
    else{
        editor.putString(mData.mVisitKey, "nein");
        editor.commit();
        mGUI.mBtnVisit.setChecked(false);
    }
    mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}

EDIT: I tried it another way.. I thought it would be better but doesnt work as well..

public void changeVisitStatus(){
    SharedPreferences visitStatus = mData.getVisitStatus();
    SharedPreferences.Editor editor = visitStatus.edit();

    if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
        editor.putString(mData.mVisitKey, "ja");
        editor.putBoolean("isChecked", true);
        editor.commit();

    }
    else{
        editor.putString(mData.mVisitKey, "nein");
        editor.putBoolean("isChecked", false);
        editor.commit();

    }
    mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}

and put this one into my onCreate(Bundle savedInstanceState) in my Activity

mGUI.mBtnVisit.setChecked(mData.getVisitStatus().getBoolean("isChecked", false));
PeterP
  • 3
  • 4

2 Answers2

0

Try like this:

   public void putBooleanInPreferences(boolean isChecked,String key){
        SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, isChecked);
        editor.commit();        
    }
    public boolean getBooleanFromPreferences(String key){
        SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
        Boolean isChecked = sharedPreferences.getBoolean(key, false);
        return isChecked;       
    }

and in onCreate()

 CheckBox checkBox = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        checkBox = (CheckBox) findViewById(R.id.my_check_box);
        boolean isChecked = getBooleanFromPreferences("isChecked");
        Log.i("start",""+isChecked);
        checkBox.setChecked(isChecked);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                Log.i("boolean",""+isChecked);
                TestActivity.this.putBooleanInPreferences(isChecked,"isChecked");
            }
        });
    }

Hope this may help you!

Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
0

You're showing us only the code for changing the status, probably called from OnClick listener for the check box.

You should also add code that only reads status from SharedPreferences and sets check box state according to that (could be same code, but the if condition negated).

You need to call that code from OnCreate event.

public void setVisitStatus(){
    SharedPreferences visitStatus = mData.getVisitStatus();
    mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
velis
  • 8,747
  • 4
  • 44
  • 64
  • No i've got no OnClick listener for my checkbox. I thought i could be handle anything about the changig status (if that states - so the checkbox will state too?) I dont understand what do you mean by only reads status and sets check box state? ... do you refer to the first try or the edit? @velis – PeterP Sep 01 '14 at 11:36
  • how do I read only the satus from the SharedPreferences.. sorry for my questions.. im just a beginner and try to learn – PeterP Sep 01 '14 at 12:16
  • I have written the code for you in my answer. Place those two lines in your OnCreate method. – velis Sep 02 '14 at 04:28