Skip to content Skip to sidebar Skip to footer

Listview Item State_selected Is Lost On Real Device

I have a problem with a simple ListView on a Samsung phone. I create a new ListView programmatically. The items are simple TextView. I put a listener on my list view: listView.setO

Solution 1:

I don't have a samsung device on hand (Galaxy Nexus dosen't count, it has plain Android and it works quite fine with your example) so I cannot test my assumptions but it seems that ListView drops selected state of the item after it has been released. You can check it with HierarchyViewer (use Romain Guy's ViewServer if your phone isn't rooted).

It is dangerous to rely on selection having a touchscreen because of TouchMode (see http://android-developers.blogspot.ru/2008/12/touch-mode.html). In two words: you don't have a concept of selection (or focus, btw) when a user is interacting with a touchscreen. Emulator usually has D-pad so it may have slightly different behavior.

So my suggestion for you is to use state_checked instead of state_selected. Android has CheckedTextView that may help. Just call ListView's setItemChecked. This solution also has nice properties of keeping checked item position between configuration changes and automatical unchecking previously checked item when other item is pressed (if CHOICE_MODE_SINGLE is used).

If it is not acceptable and you need to stick with state_selected then you may wrap your TextView into LinearLayout, it should prevent selection from disappearing. But don't forget that ListView reuses the same view for other list items when it goes out of screen, so you need to track selection state in your adapter to properly set it.

Solution 2:

I can confirm, I experienced a similar bug only on Samsung devices (Nexus, S3, Note2). Listview item has the default background colour, so in my case black. But randomly it becomes red. If I set the listviewitem backgroundcolor explicitly to black, I don't experience the bug. Honestly, I don't know how to workaround in your case, but to define colours of all state may help you .

Post a Comment for "Listview Item State_selected Is Lost On Real Device"