简体   繁体   中英

Send checkbox to another Activity

I have a TO DO List, and I when the checkbox is checked i want the item in the ListView to go to another activity in the app. This is my adapter code:

public class MyItemAdapter extends ArrayAdapter<String>  {

private final Context context;
private final String[] values;

    public MyItemAdapter(Context context, String[] values) {
    super(context, R.layout.item_list, values);
    this.context = context;
    this.values = values;
}

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.item_list, parent, false);
    CheckBox textView = (CheckBox) rowView.findViewById(R.id.checkBox1);
    textView.setText(values[position]);

    return rowView;
  }

public static SparseBooleanArray getCheckedItemPositions() {
    // TODO Auto-generated method stub
    return null;
}

public static void remove(Object pos) {
    // TODO Auto-generated method stub

}

public void changeData(String[] items) {
    // TODO Auto-generated method stub

}

public static void setAdapter(MyItemAdapter mAdapter) {
    // TODO Auto-generated method stub

}

}

And this is the code i have done so far:

check = (CheckBox) findViewById(R.id.checkBox1);
      check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){

            }

What code should I put for it the item to go to a different activity.

Please Help.

You need to start your second activity with an intent, and use putExtra to pass additional information to the new Activity

Intent intent = new Intent(this, MyActivityClass.class);
intent.putExtra("checkbox_state", check.isChecked());
startActivity(intent);

In your second activity, you receive this value by getting the intent, and stripping the additional information

Intent intent = this.getIntent();
checked = intent.getBooleanExtra(EXTRA_IS_BUILTIN);

Send the boolean value of the checkbox via an Intent . You can do so with the putExtras() method on the Intent:

intent.putExtra("checkboxValue", isChecked);
Intent i = new Intent(this, newActivity.class); 
Bundle b = new Bundle(); 
b.putString("checked", isChecked); 
i.putExtras(b); 
startActivity(i); 

You should use a bundle so that you can also use it to check for savedInstanceState when the Activity gets reloaded after orientation change. Use the ternary operator to check for savedInstanceState or use the Bundle

I faced a little confusion on your question.

Your header says: Send checkbox to another Activity

Answer: Then you can follow all answers given here and ignore my answer.

But your body part says some other thing which is :

when the checkbox is checked i want the item in the ListView to go to another activity in the app

For this you have to modify a little in other answers and send the selected item of listview when checkbox checked. something like :

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            String mData = your_list.getItemAtPosition(position); //Or you can use getSelectedItem()
            Intent intent = new Intent(context, other_activity.class);
            intent.putExtra("ListView Item", mData );
            startActivity(intent);
        }

For get data in another activity just follow as Merlevede said..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM