将以编程方式创建的复选框的值传输到下一个活动中?

时间:2017-09-26 17:51:44

标签: android android-intent checkbox spinner getvalue

在我的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类中抓取)。我的CheckBoxes没有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.

    }
}

1 个答案:

答案 0 :(得分:0)

OnClickListener中的for不会被执行,因为count很可能是0,因为它在膨胀后立即初始化ll

BodyParsers.TolerantText

在添加任何复选框之前。 您应该在clicklistener中直接使用 ll.getChildCount()或初始化计数,并在创建复选框的for循环之后设置侦听器。