I really can't make this up so I'd be thankful for any hint. I must make some mistake here (4.1.2).
I have an Activity
which, in onCreate()
, sets up a subclassed ArrayAdapter
for ListView
items which render as a Checkable
ViewGroup
.
The Activity
already utilizes a NonConfiguration
mechanism to re-build the Adapter upon orientation change. However, it's currently not storing the ListView
's getCheckedItemPosition()
because I feel it shouldn't be necessary (details below).
Interestingly, what I'm observing is the following.
- The Activity is rendered.
- The user checks a ListView item.
- The ListView item is displayed in a checked state.
- The
onItemClickListener
callsgetCheckedItemPosition()
for theListView
and gets a correct result. - The user changes the screen orientation.
onCreate()
re-builds the 'ListView' and it displays just like before (afteronCreate()
; see below).onCreate()
callsgetCheckedItemPosition()
and gets-1
despite theListView
showing the correcly checked item
Upon further examination, the following details emerge.
onCreate()
:- get
ListView
resource - build
MyAdapter
- set
MyAdapter
as adapter forListView
getCheckedItemPosition()
returns-1
- get
- after
onCreate()
:MyAdapter.getView()
is being calledCheckableViewGroup.setChecked()
is called with the correctchecked
value- the last two steps will be repeated for all items
As said before, I'm relying on the Android feature that View
objects save their state if they have an ID assigned. I'd say this is the case since there must be some object out there which sets the correct checked
status for all the list entries. (By the way, CheckableViewGroup
also overrides on{Save,Restore}InstanceState()
but that won't be called regardless whether or not it has an ID assigned (presumably because it never gets attached to the layout root?).
So it looks as if the ListView
at the same time knows and does not know its getCheckedItemPosition()
? Or am I on the wrong track?
public final class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
itemList = (ListView)findViewById(R.id.ma_list);
listAdapter = new MyAdapter();
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(
new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int pos, long id) { checkForm(); }
}
);
listAdapterSetup();
// this is where itemList.getCheckedItemPosition() returns -1
// when the listView has been re-built from NonConfiguration data:
checkForm();
}
}