0

When a certain condition is met in my code, I need to update only a single View within each row of my ListView (each row contains multiple views). Is there a way to target only the updated Views?

I have tried setting a flag and then calling notifyDataSetChanged(), however, how do I know when notifyDataSetChanged() has finished calling getView(...) for the currently visible items?

Daiwik Daarun
  • 3,804
  • 7
  • 33
  • 60
  • The currently visible views will be updated immediately. However, the flag should preserve its state as the adapter might redraw views as they go in and out of the screen. – Shivam Verma Jul 06 '14 at 03:31
  • For implementation reasons, the flag should AND needs to be disregarded immediately after the views update. The flag is indicating that only some of the rows contents need to be updated. – Daiwik Daarun Jul 06 '14 at 03:39

2 Answers2

0
Boolean updateViewsFlag = true;

// inside getView() :

if(updateViewsFlag) {
    //Suppose you want to update some text in a textView
    textView.setText("updatedText");
    //Also, update the corresponsing value in the Collection, 
    //you're using to generate the ListView. 
    //Consider an ArrayList<String> for this case.
    arrayList.set(position, "updatedText");
} else {
    textView.setText(arrayList.get(position));
}

To find out if notifyDataSetChanged() is complete or not, check if the position of the current view is equal to the size of the ArrayList. If it is, it means all views have been refreshed and you can set the flag to false. But keep in mind that getView() will be executed multiple times so make sure you dont change the flag value unnecessarily.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
0

I think it's not safe to assume that you'll get the exact same view object in getView method. How about using the solution below?
How can I update a single row in a ListView?

Actually getChildAt also is not so much safe because the description is not accurate, but I think if this works, android team doesn't need to change the behavior.
http://developer.android.com/reference/android/view/ViewGroup.html#getChildAt(int)

Community
  • 1
  • 1