In my Activity_fruits
there is a spinner with fruit names like "Apple, Pineapple, ..., etc.). When the user selects one of the items in the spinner, a series of check boxes is generated.
These checkboxes are names of dishes that come from my Dishes.java
class. Every different fruit selected by the user from the spinner will create different dish checkboxes depending on which dishes corresponds to which fruit.
My question is: how do I pass the value/text of these generated check boxes into my next activity Activity_checkout
when they are selected/checked by the user?
I have code below that seems to run fine BUT the value/text of the checked checkboxes (contained in the test[0]
intent) isn't being transferred to the next activity.
Below is my views and controls declarations:
public class Activity_fruits extends Activity {
// Instantiate Fruits class
private Fruits fruits = new Fruits();
private Dish dish = new Dish();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruits);
final String passToNextActivity = getIntent().getStringExtra("selectedDish");
final String fruitStr = spnFruits.getSelectedItem().toString();
final LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
final int count = ll.getChildCount();
final String[] test = {""};
...
And below is how I programmatically generated check boxes for dishes (grabbed from my Dish.java class). There is no xml layout for my CheckBoxes:
// Create different dish checkboxes for each food selection in the spinner
spnFruits.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String fruitStr = (String) adapterView.getItemAtPosition(i);
TextView txtRestaurant = (TextView) findViewById(R.id.txtFruits);
//This TextView is just to confirm the fruit that the user selected
txtFruit.setText("You selected the Fruit: " + fruitStr);
final List<String> listFruits = fruits.getFruits(fruitStr);
// Clears the layout everytime the user selects another fruit from the spinner
ll.removeAllViews();
for (int j = 0; j < listFruits.size(); j++) {
final CheckBox cbDishes = new CheckBox(Activity_fruits.this);
cbDishes.setText(listFruits.get(j));
cbDishes.setTag(listFruits);
ll.addView(cbDishes);
////////////
cbFruits.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int x = 0; x < count; x++) {
View v = ll.getChildAt(x);
if (v instanceof CheckBox){
test[0] = ((CheckBox) v).getText().toString(); } //My problem: This code is not storing any string at all
}
}
});
////////////
}
}
});
/////////////
Button btnNextActivity = (Button) findViewById(R.id.btnNextActivity);
btnNextActivity.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent = new Intent(Activity_fruits.this, Activity_checkout.class);
intent.putExtra("selectedDishes", test[0]); //My Problem: The test[0] is supposed to contain all user selected check boxes but it doesn't.
startActivity(intent);
}
});
}
}
And my next activity Activity_checkout
is:
public class Activity_checkout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
final String dishes = getIntent().getStringExtra("selectedDishes");
TextView txtSelectedDish = (TextView) findViewById(R.id.txtSelectedDish);
txtSelectedDishs.setText("Your selected dishes are: " + dishes.toString()); //My Problem: food.dishes.toString() is not returning a value meaning the checkbox value from Activity_fruits wasn't successfully transferrred over.
}
}
Aucun commentaire:
Enregistrer un commentaire