getting View for ListView item / reverse order on 2.2; works on 4.0.3
android, android-adapter, android-listview, android-view
Solution
OK. so this is what is happening :
On a clean `ListView` When I register `onItemClickListener` and perform clicks adapter `getView` method for views is not called. This is what I would expect.
If I set a `mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE)` this makes `getView` methods run right after click is registered. This is also what is expected.
However the difference between 2.2 and 4+ versions is that : on 2.2 : when this refresh happens `getView` operates on a reversed list of views (see my question) on 4+ (possibly API 11+) : `getView` operates on normal order of list
What is the most interesting part is that when I delay call to getViewForListPosition by 10ms everything works fine and adapter has proper list of views (normal order again). So it seems that the order of views is reversed only during adapter refresh with `CHOICE_MODE_SINGLE`
To solve this problem I dont change listview mode to `CHOICE_MODE_SINGLE` so that adapter doesn't fire during click. I set bg/graphics for clicked item on my own inside `onItemClicked`
hope that it saves someone some time :)
Problem
I have a ListView which shows items from ArrayAdapter. I want to animate the view when it is cliked. The problem is that on different versions of android I am getting different views (see below) I'm getting the view from ListActivity using this method : ``` private View getViewForListPosition(int position) { int firstPosition = mList.getFirstVisiblePosition() - mList.getHeaderViewsCount(); int wantedChild = position - firstPosition; ZLog.d(LOG,"getViewForListPosition , position : " + position + ", wantedChild : " + wantedChild + ", view hash : " +mList.getChildAt(wantedChild).hashCode()); for(int i = mList.getChildCount(); i>0; i--){ ZLog.d(LOG, "pos : " + (i-1) + ", hash : " +mList.getChildAt(i-1).hashCode()); } return mList.getChildAt(wantedChild); } ``` So that on Android 4.0.3 & 4.2.2 phone I get : ``` getViewForListPosition , position : 3, wantedChild : 3, view hash : 1101734248 pos : 5, hash : 1104109360 pos : 4, hash : 1104254936 pos : 3, hash : 1101734248 pos : 2, hash : 1104876880 pos : 1, hash : 1104862296 pos : 0, hash : 1104793008 ``` then when item is clicked my adapter `getView` method runs for each view : ``` getView, position : 0, convertView is notnull, cv hash1104793008 getView, position : 1, convertView is notnull, cv hash1104862296 getView, position : 2, convertView is notnull, cv hash1104876880 getView, position : 3, convertView is notnull, cv hash1101734248 getView, position : 4, convertView is notnull, cv hash1104254936 getView, position : 5, convertView is notnull, cv hash1104109360 ``` so you can see everything is fine and works as expected. However when I run this on Android 2.2 these are the results I am getting : ``` getViewForListPosition , position : 3, wantedChild : 3, view hash : 1205607672 pos : 5, hash : 1205730120 pos : 4, hash : 1205712904 pos : 3, hash : 1205607672 pos : 2, hash : 1206547728 pos : 1, hash : 1206483960 pos : 0, hash : 1207864856 getView, position : 0, convertView is notnull, cv hash1205730120 getView, position : 1, convertView is notnull, cv hash1205712904 getView, position : 2, convertView is notnull, cv hash1205607672 getView, position : 3, convertView is notnull, cv hash1206547728 getView, position : 4, convertView is notnull, cv hash1206483960 getView, position : 5, convertView is notnull, cv hash1207864856 ``` so as you might have noticed `getViewForListPosition` will give me back the view which adapter uses for position 2 You might have also noticed that either the `Adapter.getView` or `ListView.getChildAt` is returning items in reverse order which causes this issue. What could be the reason for this behaviour? (I'm not doing anything fancy in my adapter) I will be thankful for any kind of hint. Thanks!