0

If the checkbox is checked I want to output text in another activity can save its state. I know I will need to put in a shared preferences somewhere but I'm new to this so I'm not sure how to do it

public class Bookingscreen extends AppCompatActivity {

CheckBox Lothianside;
CheckBox Mckeowns;
CheckBox Loch;
OnClickListener checkboxListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bookingscreen);
    Lothianside =(CheckBox)findViewById(R.id.checkBox3);
    Mckeowns =(CheckBox)findViewById(R.id.checkBox4);
    Loch =(CheckBox)findViewById(R.id.checkBox5);
    checkboxListener = new OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Lothianside.isChecked()){

            }

            if (Mckeowns.isChecked()){

            }

            if (Loch.isChecked()){

            }

        }
    };

    Lothianside.setOnClickListener(checkboxListener);
    Mckeowns.setOnClickListener(checkboxListener);
    Loch.setOnClickListener(checkboxListener);




}



}
ablnoozy
  • 25
  • 6
  • http://stackoverflow.com/a/10238647/3790150 try this – saeed Mar 19 '16 at 10:07
  • Also, it's probably better for you to use setOnCheckedChangeListener for your checkboxes instead of onClick listener – Wukash Mar 19 '16 at 10:07
  • Is this essentially a preference screen? You might want to look at `PreferenceActivity` and/or `PreferenceFragment`. They give you nice easy ways to store lots of checkbox values and things like that. – Mark Smith Mar 19 '16 at 10:09

2 Answers2

0

You can use preferences with the below methods, sorry if there are any mis-spellings, I typed it quickly.

public void save(String key, String value){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.edit().putString(key, value).commit();
}

public String retrieve(String key){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    return prefs.getString(key, ""); //the second parameter is the default if no value is found
}

In your code:

if (Lothianside.isChecked()){
    save("lothianKey", "text that you want to save when lothianside.ischecked()");
}

Though I dont like this, you should set an onCheckChanged listener:

Lothianside.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
         //do stuff here if the box is checked or unchecked
       }
   }
);
0

dont need shared preferences.... create 3 global char variable

 char i ="f";
 char j="f";
 char k="f";

then

       if (Lothianside.isChecked()){

         i="t";

        }
        else{
         i="f";
        }

        if (Mckeowns.isChecked()){

            j="t";

        }else{
              j="f";
           }


        if (Loch.isChecked()){

             k="t";

        }
         else{
              k="f";
            }

now when your are going to call new activity you write that code in this way...

 Intent intent = new Intent(current_activity_name.this, new_activity.class);   
intent.putExtra("i_value", i);
intent.putExtra("j_value", j);
intent.putExtra("k_value", k);
startActivity(intent);

now in your new_activity you can access this this variable in this way --

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_activity_main);

    Bundle extras = getIntent().getExtras();
    int i=extras.getString("i_value");
    int j=extras.getString("j_value");
    int k=extras.getString("k_value");
}

thst it.. :) pls dont forget to make that answer right if it is helpfull.happy coding

snehasish
  • 171
  • 2
  • 10