Search code examples
androidlistviewandroid-4.2-jelly-bean

Listview selection isn't displayed on Android jellybean


I have a listview, that has been working as expected for some time. Recently a user updated their device software to JellyBean, and now the listview selection doesn't update when pressed.

In the background, the onTouchEvent() method fires, (and in-turn the adapter's onItemClick()) and the selection is set 'under the hood', it simply doesn't display as selected. However, if the device is now rotated and the orientation changed, the selection highlight is displayed on the redraw!

I've searched other issues and the closest I could find was an onAttachedToWindow() method being overridden and not calling it's super implementation, however this is the not the case here!

Has anybody else seen a similar issue on JellyBean, and know of a solution?


Solution

  • A Better solution to this problem would be to use invalidateViews() function of listview .

    ListView.invalidateViews() is used to tell the ListView to invalidate all its child item views (redraw them). While Adapter.notifyDataSetChanged() , is to tell the observer of the adapter that the contents of what is being adapted have changed.

    In this particular case ListView.invalidateViews() seems like a better option as your dataset is not updated and you just need to redraw the views.

    insert listview.invalidateViews() in your OnItemClick and you are good to go.

    public void onItemClick(AdapterView<?> av, View v, int position,long id) {
                // TODO Auto-generated method stub
                        listview.invalidateViews();
                /*  YOUR   CODE 
                                    GOES   HERE    */          
            }
        });
    

    You can use the same function for GridView as well .