Why doesn't "notifyDatasetChanged()" notify all visible items?
Asked Answered
C

0

6

Background

Note: I've been using listViews for ages, so I'm very familiar with how to work with adapters, ViewHolders, etc... so this issue doesn't make sense for me.

This time I got into a very weird behavior that for some reason occur only on 2 devices:

  1. LG G2, with Android 4.2.2

  2. Xperia J, with Android 4.1.2

The problem

The listView has its own BaseAdapter, which on some events I update its items collection within it (on the UI thread) , and call notifyDatasetChanged().

The problem is that on the problematic devices, calling this function calls getView only on some of the visible rows of the listView. For example, if rows 1,2,3,4,5 are visible, and I call notifyDatasetChanged , and I put log on getView, I can see it goes over 1,2,4,5 (skipped row 3).

Some clues

  • it always skips the row that the user clicks inside of it (each row has clickable views ) that triggers the notifyDatasetChanged calls .

  • Other devices handle the listview just fine : 2 Nexus 4 devices (each with a different version), Galaxy S2, and Galaxy S4 .

  • It doesn't matter if I call notifyDataSetChanged once or twice .

  • Even though I use my own BaseAdapter , I didn't change the way the ListView work. I didn't extend ListView.

  • For the BaseAdapter class, I've overriden the next functions:

    • getViewTypeCount -returns 2
    • getItemViewType - returns 0 or 1, depending on the type of the row
    • getCount
    • getItem
    • getItemId - I don't need it, so I made it always return 0 (it won't help making it return the position instead).
    • getView
    • areAllItemsEnabled and isEnabled - always returns false , as all rows have inned views that only they need to handle clicking .

    I didn't override any other method. Of course I created helper functions, but that's it.

The question

Why does it occur?

How come it occur only to some devices?

How can I solve this?

Is it possible it's a bug on some roms ?

If so, is there a good workaround for this, or should I really call getView on the problematic row (which works fine BTW, but it's just weird that I do this) ?

Catchment answered 11/8, 2014 at 12:29 Comment(7)
Have you provide two types of list items view ?Audriaaudrie
Can you please check this it might be help you : #19635720Audriaaudrie
@Haresh Yes, I used the correct type inside getView, and provided different types of views (though even if I used the same type, it should have worked). About the link, this seems like the same workaround I've offered, but it doesn't make sense that it's needed, since the documentation say: "Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself." . It doesn't require that the number of items will change.Catchment
getItemId() - AbsListView uses it even if you don't so return at least position there.Selfexistent
@Selfexistent I actually tried it, and it didn't help. :(Catchment
@androiddeveloper Found any solution. I am in same situation :(Placement
@YoloYolo Best thing is to use RecyclerView. It's not the same, but it should work.Catchment

© 2022 - 2024 — McMap. All rights reserved.