0

If I programmatically create checkboxes as follows:

private Fruits fruits = new Fruits();

final String color = ...  //user input from another control
final List<String> listFruits = fruits.getFruits(color);
final LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

...

for (int i = 0; i < listFruits.size(); i++)
{
   final CheckBox chkFruits = new CheckBox(Main_Activity.this);
   chkFruits.setText(listFoods.get[i]);

   ll.addView(chkFruits);

   //insert code here on how to pass data of checked checkboxes to my next Activity_2
}

And my Fruits class is as follows:

public class Fruits {
   List<string> getFruits(String color) {
      List<String fruits = new ArrayList<String>();

      if (color.equals="red"){
         fruits.add("Apple");
         fruits.add("Strawberry");
      }
      else if (color.equals="yellow"){
         fruits.add("Banana");
         fruits.add("Pineapple");
      }

      return fruits;
   }
}

How do I check for which generated checkboxes have been checked by the user AND THEN pass it's information (i.e. setText(listFoods.get[0] will give "Apple" if color="red") to my next Activity_2?

I tried using Intent:

final String[] passThis = new String[1];
Button btn = (Button) findViewById(R.id.btn);

...

btn.setOnClickListener(new View.OnClickListener(){
...
Intent intent = new Intent (getBaseContext(), Activity_2.class);
intent.putExtra("checkedFruits", passThis[0]);
startActvity(intent);

But it returns a NULL. How do I get the text/value of my generated checkboxes into my next Activity_2?

EDIT: How do I push whatever checked CheckBoxes into my passThis[] array so that I can pass them onto my next Activity?

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
5120bee
  • 689
  • 1
  • 14
  • 36
  • Possible duplicate of [Passing data between activities in Android](https://stackoverflow.com/questions/2965109/passing-data-between-activities-in-android) – Vishal Yadav Sep 28 '17 at 05:00

1 Answers1

0

Create a list of texts of checkboxes checked and need to add clickListener on each checkbox. Try something like this:

   List checked<String> = new ArrayList()// stores checked checkboxes text


   for (int i = 0; i < listFruits.size(); i++)
   {
 final CheckBox chkFruits = new CheckBox(Main_Activity.this);
  chkFruits.setText(listFoods.get(i));
  chkFruits.setTag(i); // to identify checkboxes
  //add click listener to your checkboxes
  chkFruits.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            CheckBox checkbox=(CheckBox)v;
            if(checkbox.isChecked())
            {
                int pos=(int)checkbox.getTag();
                checkbox.setChecked(false);
                checked.remove(pos);

            }else
            {
                checkbox.setChecked(true);
                checked.add(checkbox.getText());
            }

        }
    });      
  ll.addView(chkFruits);

}

you can pass checked list to next Activity.

N.Moudgil
  • 709
  • 5
  • 11