Child View items are not refreshing

948 Views Asked by At

Here's a code for expandable listview in which each grouo has a checkbox and a textview. There's a Select All checkbox, on click it should change the states of all the child view's checkboxes but its not happening. Can someone explain why??

MainActivity.java

public class MainActivity extends ActionBarActivity implements OnClickListener {

     ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        static CheckBox selectAll;
        HashMap<String, List<String>> listDataChild;

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

            // get the listview
            expListView = (ExpandableListView) findViewById(R.id.expandableListView1);
            selectAll=(CheckBox)findViewById(R.id.checkBox1);
            // preparing list data
            prepareListData();

            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

            // setting list adapter
            expListView.setAdapter(listAdapter);
            selectAll.setOnClickListener(this);

        }

        /*
         * Preparing the list data
         */
        private void prepareListData() {
            listDataHeader = new ArrayList<String>();
            listDataChild = new HashMap<String, List<String>>();

            // Adding child data
            listDataHeader.add("Top 250");
            listDataHeader.add("Now Showing");
            listDataHeader.add("Coming Soon..");

            // Adding child data
            List<String> top250 = new ArrayList<String>();
            top250.add("The Shawshank Redemption");
            top250.add("The Godfather");

            List<String> nowShowing = new ArrayList<String>();
            nowShowing.add("The Conjuring");
            nowShowing.add("Despicable Me 2");


            List<String> comingSoon = new ArrayList<String>();
            comingSoon.add("2 Guns");
            comingSoon.add("The Smurfs 2");


            listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
            listDataChild.put(listDataHeader.get(1), nowShowing);
            listDataChild.put(listDataHeader.get(2), comingSoon);
        }

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            CheckBox cb=(CheckBox)listAdapter.getChildView(0, 0, false, null,         null).findViewById(R.id.cb_child);
            cb.setChecked(true);
            ((BaseExpandableListAdapter) listAdapter).notifyDataSetChanged();
        }

}

ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter  {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    private HashMap<String, List<String>> _listDataChild;


    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;

    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.textView1);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }   

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.expandablelisttest.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ExpandableListView
        android:layout_marginTop="100dp"
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" >

    </ExpandableListView>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="21dp"
        android:text="Select All" />

</RelativeLayout>

child_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <CheckBox
        android:id="@+id/cb_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:text="CheckBox" 
        android:checked="false"/>

</RelativeLayout>

group_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="14dp"
        android:text="TextView" />

</RelativeLayout>
2

There are 2 best solutions below

5
On BEST ANSWER

Method getChildView(..) is used by adapter to populate the expandable child view under group.

I can encourage you to create your own ExpandableListAdapter which extends from BaseExpandableListAdapter (If you have 1 group = 1 item, you can simply use only 1 arrayList with your own POJO object model)

And finnaly, for a full explanation, when you call notifyDataSetChanged(), the list refresh view with the Adapter's data (example get each boolean and use there value for check or uncheck checkbox)

1
On

You can't do it this way because your ExpandableListAdapter only knows the items strings and not the checkbox.
You have to modify your ExpandableListAdapter to have access to the checkbox in it.

This might help you ---> http://www.survivingwithandroid.com/2013/01/android-expandablelistview-baseexpandablelistadapter.html.
You need to create a structure that contains the objects you want in each items of your list.