在我Activity_fruits
有一个带有水果名称的微调器,例如“Apple,Pineapple,......等)。当用户选择微调器中的一个项目时,会生成一系列复选框。
这些复选框是来自我Dishes.java
班的菜肴的名称。用户从微调器中选择的每种不同水果都会根据哪些菜对应于哪种水果创建不同的菜式复选框。
我的问题是: 当用户选择/检查这些生成的复选框时,如何将它们的值/文本传递到我的下一个活动中? Activity_checkout
我下面的代码似乎运行良好,但选中的复选框的值/文本(包含在test[0]
意图中)没有被转移到下一个活动。
以下是我的观点和控制声明:
公共类 Activity_fruits 扩展 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 = {""};
...
下面是我如何以编程方式为菜肴生成复选框(从我的 Dish.java 类中获取)。我的复选框没有 xml 布局:
// 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);
}
});
}
}
我的下一个活动Activity_checkout
是:
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.
}
}