Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant get the values from dynamically added check box

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.

like image 622
Madhu Avatar asked Apr 04 '14 06:04

Madhu


1 Answers

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

        }

        }
    });
like image 61
kstachniuk Avatar answered Nov 15 '22 10:11

kstachniuk