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?