android notifyDataSetChanged for ExpandableListView not working
Asked Answered
M

4

6

In my app I am working with ExpandableListView. I am using adapter which extends BaseExpandableListAdapter. I want to refresh the ExpandableListView.

My ExpandableListView consists of items with delete button which links with database. If I'll press the delete button that item is permanently deleted from db. But the listview is not refreshing at the same time. If I again run that activity its refreshing but not at same time. I am using

mAdapter.notifyDataSetChanged();

but its not working as I want. Why?

Matherly answered 19/10, 2011 at 7:31 Comment(2)
did you re-queried the Cursor also..?Poppy
as i only deleted the item , for that cursor is not required. So i am not using thatMatherly
R
9

I just called super on the following overridden method of the BaseExpandableListAdapter and since then the notifyDataSetChanged() works.

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);    
    }
Roast answered 28/2, 2012 at 16:25 Comment(5)
Not sure how as ExpandableListAdapter is an interface.Colleague
I think it's a bit late but actualy I used the class : BaseExpandableListAdapter as the list adapter..Roast
Ahh OK that would make sense, thanks for updating the answer.Colleague
So, you must have been overriding that method and NOT calling super. previously?Fadiman
I don't remember overriding that method either, but it looks like Android Studio implemented it for me without adding the super for some reason. Thanks for pointing this out - I was stuck on this one for a while :)Soapbox
K
4

you can try to additionally call invalidateViews() on your ExpandableListView when refreshing.

Kathrynekathy answered 19/10, 2011 at 7:43 Comment(9)
where i ll write it? bcz i want on delete button click refresh should be happend.Matherly
whenever the content of your ExpandableListView was changed. So after you deleted some entry for exampleKathrynekathy
but there invalidateViews() not availableMatherly
this method must be called on the ExpandableListView object not on your adapter. According to the android java doc ExpandableListView inherits this method from the super class AbsListView: developer.android.com/reference/android/widget/…Kathrynekathy
ya i am using that object only but its not availableMatherly
my class extends BaseExpandableListAdapterMatherly
The method is not available for the BaseExpandableListAdapter but for your ExpandableListView where you called setAdapter(...). So for example if you called mList.setAdapter(mAdapter) and you want to update the list view you have to call mList.invalidateViews() and not mAdapter.invalidateViews(). Before calling invalidateViews() you should also call mAdapter.notifyDataSetChanged()Kathrynekathy
ya i did that, but it not refreshing at d same timeMatherly
I can confirm that this did not work when calling myExpandableList.invalidateViews() after updating the adapterMineral
C
0

You have to wait until the whole viewtree has been drawn and then you can call notifyDataSetChanged()

"A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change.... A ViewTreeObserver should never be instantiated by applications as it is provided by the views hierarchy. Refer to getViewTreeObserver() for more information."

http://developer.android.com/reference/android/view/ViewTreeObserver.html

 final View v = convertView;
        ViewTreeObserver vto = convertView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            public void onGlobalLayout() {
                v.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                notifyDataSetChanged();     
}}); 
Converge answered 16/4, 2016 at 19:30 Comment(0)
B
0

If you are using Expandable lists of Big Nerd Ranch, please note that traditional notifyDataSetChanged() of RecyclerView.adapter is not working.

Instead Expandable RecyclerView provides a set of notify methods with the ability to inform the adapter of changes to the list of ParentListItems.

// Parent Changes
notifyParentItemInserted(int parentPosition)
notifyParentItemRemoved(int parentPosition)
notifyParentItemChanged(int parentPosition)
notifyParentItemRangeInserted(int parentPositionStart, int itemCount)

// Child Changes
notifyChildItemInserted(int parentPosition, int childPosition)
notifyChildItemRemoved(int parentPosition, int childPosition)
notifyChildItemChanged(int parentPosition, int childPosition)

For additional specifics visit https://bignerdranch.github.io/expandable-recycler-view/

Bunce answered 18/9, 2016 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.