Cant get the values from dynamically added check box

那年仲夏 提交于 2019-12-21 17:29:03

问题


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

This is my code

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());
                    }

                }

            }
        });

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.

Thanks in advance.


回答1:


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());
                    }
                }

        }

        }
    });


来源:https://stackoverflow.com/questions/22855276/cant-get-the-values-from-dynamically-added-check-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!