0

I'm using two different views in a fragment that contain a ListView. These ListViews use the same ListAdapter. In landscape, I show the listview in the fragment and in portrait, there is a button that shows a dialog containing the listview. My fragment is setRetainedState(true) and I don't recreate my listAdapter if it's not null.

I've set a selector for the background color of my ListItem, (if state_activated => red background; default => transparant). When initializing my ListView (on change to landscape and on displaying dialog with listview) I set the ChoiceMode to CHOICE_MODE_SINGLE.

This way, if I call 'view.setActvated(true)' in my OnItemClickListener of my ListView, the right item gets the right background, and everything works fine.

My next step was storing a reference of the activated view in my Adapter. In my getView() method I check if the view i'm building is the one I've stored. If so, I call 'convertView.setActivated(true)' and return the convertView. But when my ListView gets recreated with the same adapter, it does not set the correct view (or any other view) activated, even though setActivated(true) was called for 1 specific View (checked with Log.d)

I only get it right if I call 'view.setActivated(true)' in the OnItemClickListener

dumazy
  • 13,857
  • 12
  • 66
  • 113
  • When orientation changes activity restarts, so make some changes in your manifest file to avoid restarting of activity use android:configChanges="orientation" – Himanshu Joshi Oct 23 '13 at 09:46
  • 1
    That's the really bad way to 'solve' this, this will avoid my fragment from getting the right view because portrait and landscape are not the same. I would have to handle all orientation changes myself. It's a 'solution' that's suggested a lot but it's not the right solution. – dumazy Oct 23 '13 at 09:51

0 Answers0