I know that this has been asked a number of times, but none of the answers work for me.
I have an activity which hosts a single selection ListView (- no support library anywhere). The list is populated from database entries using a custom adapter, implementing the ListAdapter interface, at activity creation time in OnCreate.
The list view is a custom layout holding two TextViews within a LinearLayout.
I have a state list for handling the visual indications :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:drawable="@color/orange_selected" />
<item android:state_pressed="true" android:drawable="@color/orange_pressed" />
</selector>
When an item is clicked/tapped, I capture it within the OnItemClickedListener listener and then select the row in this manner :
v.post(new ViewSelector(v));
Where ViewSelector is a runnable which simply does this :
view.setSelected(true);
When I change orientation, I store the currently selected row in the outgoing bundle through onSaveInstanceState(Bundle), and pick it back up in onCreate, passing it into the custom adapter.
The adapter will then check the selected row index when processing the getView call :
if(SelectedRow == position)
v.post(new ViewSelector(v));
Although I call the same method on the rows view to select the row (via a post), the row does not get selected.
One method would be to call View.postDelayed(action, delayMillis) - however, this is a big kludge as the right time delay will also depend on processing speed of the tablet in question.
What is the better / right way of doing this.