Custom Listview Adapter Getview Method Being Called Multiple Times, and in No Coherent Order

custom listview adapter getView method being called multiple times, and in no coherent order

This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times. In your particular case you are doing the worst thing possible with a ListView by giving it a height=wrap_content. This forces ListView to measure a few children out of the adapter at layout time, to know how big it should be. This is what provides ListView with the convertViews you see passed to getView() even before you scroll.

Adapter getView is called multiple times with position 0

Okay, I figured out the issue by expanding ListView as much possible. Meaning to say, giving a dynamic full height so that all item becomes visible.

I followed the below solution:

Calculate the size of a list view or how to tell it to fully expand

ListView - getView is called too much times

Quoting android engineer RomainGuy

This is not an issue, there is absolutely no guarantee on the order in
which getView() will be called nor how many times.

So the best you can handle is re-using the existing views (row layouts) properly.

Here is another good post.

Android custom ArrayAdapter getView method called multiple times - resetting dynamic TextView value

getView() will be called multiple times as you note. It shouldn't matter, because your array adapter is based on the state of it's internal data model (array, list of objects, whatever). getView() should be idempotent in that calling it multiple times shouldn't change the result.

You say "when you scroll and the box goes off screen the value disappears". Note sure what mean. When you scroll one of the views generated in getView() off the visible area, and when you scroll it back, the value is different? without any other information, I'd have to say that's not possible. The reason is again that unless you are modifying the internal state of the adapter, or changing the adapter, you will always generate the same view for a given position.

By the way, convertView can be null, so you want to do something like,

View v = convertView;
if (v == null) {
v = inflater.inflate(...);
}

// now use v in the rest of the method

Listview - Getview called multiple times

I think its not about textview.
The getView is always called multiple times. If you have 10 items on the screen to be shown, the getView going to be called 15 times, because the android creating views that are not on the screen. It`s good, because when the user start scrolling, it is not going to lagging.

After the user left the item, the view get`s recycle and reused by the adapter. Lets say, you have a list with 10000000 item, but you have 5 item on the screen at all time. In this case - to save power, and improve performance - the android going to create 10 list item, and this 10 item is going to recylce and refresh by content.

ViewHolder pattern
Please read this and use this patter to improve your code performance:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Google about ListView:
http://developer.android.com/guide/topics/ui/layout/listview.html

Tutorials:
http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/



Related Topics



Leave a reply



Submit