mercredi 18 novembre 2015

How to update all the Checkbox unchecked in dynamically created view using Button click?

public class CheckBoxActivity extends Activity {

    LinearLayout lay,lay1;
    ArrayList<String> list,list1;
    boolean[] checkBoxstate;
    List<Boolean> isCheckList;
    int j=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_check_box);

        lay=(LinearLayout)findViewById(R.id.lay);

        list=new ArrayList<String>();
        list.add("1");
        list.add("1");
        list.add("2");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("4");
        list.add("4");

        list1=new ArrayList<String>();
        list1.addAll(list);

        Set<String> hs = new HashSet<>();
        hs.addAll(list);
        list.clear();
        list.addAll(hs);

        System.out.println("size" + list1.size());

        isCheckList = new ArrayList<Boolean>();
        Collections.fill(isCheckList, Boolean.FALSE);
        for (int m = 0; m < list1.size(); m++) {
            isCheckList.add(Boolean.FALSE);
        }

        for(int i=0;i<list.size();i++) {

            View hiddenInfo = getLayoutInflater().inflate(R.layout.add_item, lay, false);
            LinearLayout lay2=(LinearLayout)hiddenInfo.findViewById(R.id.lay2);
            lay.addView(hiddenInfo);

            View hiddenInfo11 = getLayoutInflater().inflate(R.layout.add, lay2, false);
            lay2.addView(hiddenInfo11);

            for(j=0;j<list1.size();j++) {
                if(list1.get(j).equals(list.get(i))) {

                    System.out.println("i----"+i);

                    View hiddenInfo1 =
                            getLayoutInflater().inflate(R.layout.add_inner_item, lay2, false);
                    final CheckBox chk_box=(CheckBox)
                            hiddenInfo1.findViewById(R.id.chk_box);

                    chk_box.setId(j);
                    chk_box.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            // Cast it so we can access the public functions
                            if (chk_box.isChecked()) {
                                chk_box.setChecked(true);
                            } else {
                                chk_box.setChecked(false);
                            }

                            isCheckList.set(chk_box.getId(),
                                    chk_box.isChecked());
                        }
                    });

                    lay2.addView(hiddenInfo1);
                }
            }

        }

    }

    public void Click(View v)
    {
//        System.out.println("isCheckList"+isCheckList.size());
//
//        List<String> s=getCheckedItems();
//
//        for(int k=0;k<s.size();k++){
//            System.out.println("isCheckList"+s.get(k));
//
//        }

        for(int i=0;i<list.size();i++) {

            View hiddenInfo = getLayoutInflater().inflate(R.layout.add_item, lay, false);
            View child = lay.getChildAt(i);
            LinearLayout lay2 = (LinearLayout) child.findViewById(R.id.lay2);


            View hiddenInfo1 = getLayoutInflater().inflate(R.layout.add, lay2, false);

            for (int k = 0; k < lay2.getChildCount(); k++) {
                View child1 = lay2.getChildAt(k);
                try {
                    final TextView head = (TextView) child1.findViewById(R.id.title);
                    head.setText("Title");
                }catch(Exception e){
                    e.printStackTrace();
                }


                for (int m = 0; m < lay2.getChildCount(); m++) {

                    View hiddenInfo11 =getLayoutInflater().inflate(R.layout.add_inner_item, lay2, false);
                    View child2 = lay2.getChildAt(k);
                    try {
                        final TextView name = (TextView) child2.findViewById(R.id.name);
                        name.setText("subtitle");
                    }catch(Exception e){
                        e.printStackTrace();
                    }

                    try {
                        final CheckBox chk_box = (CheckBox) child2.findViewById(R.id.chk_box);
                        chk_box.setChecked(false);

                    }catch(Exception e){
                        e.printStackTrace();
                    }



                }

            }

        }

    }

    public List<String> getCheckedItems() {
        List<String> checkedItems = new ArrayList<String>();
        for (int i = 0; i < isCheckList.size(); i++) {
            if (isCheckList.get(i)) {
                (checkedItems).add(list1.get(i));
            }
        }

        return checkedItems;
    }



}

content_check_box.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/lay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        </LinearLayout>


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add"
    android:onClick="Click"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:clickable="true"/>
</RelativeLayout>

add_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/lay2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:orientation="vertical">


</LinearLayout>

Add.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:textSize="16sp"
        android:textStyle="bold"
        android:text="" />
</LinearLayout>

add_inner_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/lay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <CheckBox android:id="@+id/chk_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false" />

    <TextView android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

I have the following sample code for dynamically created checkbox. Once if I click the Button click, the checkbox is not checked. But the textview is changed. I am unable to check all the checkbox checked in the given view. I need some solution for this sample code.




Aucun commentaire:

Enregistrer un commentaire