简体   繁体   中英

How does notifyDataSetChanged work?

I have a question on how notifyDataSetChanged() works in a BaseExpandableListAdapter

I am updating a boolean variable, the output of which determines the output of getChildrenCount() but the number of comments visible does not change on calling it

Code:

buttonViewComments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                allCommentsVisible = !allCommentsVisible;
                notifyDataSetChanged();
            }
        });

getChildrenCount:

public int getChildrenCount(int groupPosition) {
        if(allCommentsVisible || postList.get(groupPosition).commentListSize()<=3)
            return postList.get(groupPosition).commentListSize();
        else{
            return 3;
        }
    }

notifyDataSetChanged

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself. Reference

And in your case, there isn't any underlying data changes. In order to make it work you need to change your list postList items so it will update the set and the counter will be updated along with the size of the set.

You will get some useful information about this from this link.

Android. How does notifyDataSetChanged() method and ListViews work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM