无法从动态添加的复选框中获取值 [英] Cant get the values from dynamically added check box

查看:56
本文介绍了无法从动态添加的复选框中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单击按钮时,我添加了一个复选框,最后需要通过单击提交按钮来获取所有选中的复选框值,

While click the button i have added a checkbox, finally need to get the all checked checkbox value by means of clicking submit button,

这是我的代码

mIncrementButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

                params.setMargins(20, 0, 0, 0);

                CheckBox checkbox = new CheckBox(MainActivity.this);

                checkbox.setLayoutParams(params);
                mAllText.add(checkbox);
                checkbox.setText("Checkbox" + j);

                checkboxlayout.addView(checkbox);
                j++;

                Log.d("j", "--->" + j);

            }
        });

        mGetTheVlue.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.d("Inside Button", "Inside Burron" + mAllText.size());

                for (int i = 0; i < mAllText.size(); i++) {
                    CheckBox check = mAllText.get(i);
                    if (check.isChecked()) {
                        AllCheckbox.add(check.getText().toString());
                    }

                }

            }
        });

现在我需要检查是否所有复选框都已选中并且需要获取值,
不知道这个问题是否太老了。但是我不希望从Google得到任何解决方案,请提供帮助。

Now i need to check whether all checkbox are checked and need to get the values, Dont know whether this question is so old or not. but i dint get any solution from google kindly help to get the solution.

预先感谢。

推荐答案

此外,您无需将所有CheckBox存储在Collection中。您可以从布局中获取子视图,并检查每个子视图是否为CheckBox。

Also you don't need to store all CheckBox in Collection. You can just get the child views from layout and check each of them if it is CheckBox.

    mGetTheVlue.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d("Inside Button", "Inside Burron" + checkboxlayout.getChildCount());

            for(int i=0; i<checkboxlayout.getChildCount(); i++) {
                View nextChild = checkboxlayout.getChildAt(i);

                if(nextChild instanceOf CheckBox)
                {
                    CheckBox check = (CheckBox) nextChild;
                    if (check.isChecked()) {
                                AllCheckbox.add(check.getText().toString());
                    }
                }

        }

        }
    });

这篇关于无法从动态添加的复选框中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆