Hi everyone,
I'm trying to get an ExpandableListView with a ResourceCursorAdapter
to work and it's driving me crazy.
The problem: When I let the activity manage the cursors the expanded
state of the tree is not saved between orientation changes nor when I
start an other activity over the top and go back. When I don't let the
activity manage the cursosr it works fine between screen orientation
changes, but it crashes with a "java.lang.IllegalStateException: this
should only be called when the cursor is valid" exception in the
getGroupView() method of CursorTreeAdapter when I start another
activity and return with the back button.
The relevant activity (which extends ExpandableListActivity) code
looks like this:
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbAdapter = new DbAdapter(this);
dbAdapter.open();
final Cursor groupCursor = dbAdapter.findAllGroups();
startManagingCursor(groupCursor);
setListAdapter(new MyExpandableListAdapter(this, groupCursor,
R.layout.group_list_item, R.layout.child_list_item));
registerForContextMenu(getExpandableListView());
}
and MyExpandableListAdapter just extends ResourceCursorTreeAdapter and
overrides the bindView/Group methods to return the views, and the
getChildrenCursor method as follows:
@Override
protected Cursor getChildrenCursor(final Cursor groupCursor) {
final long groupId =
groupCursor.getLong(groupCursor.getColumnIndex
("_id"));
final Cursor childCursor =
dbAdapter.findChildrenForGroup(groupId);
startManagingCursor(childCursor);
return childCursor;
}
When I debug to see what happens, I notice that the onSaveInstanceState
() actually saves the proper expanded state of the tree first, but
then my activity deactivates the (managed) cursors in onStop(),
causing the data set of the ExpandableListAdapter to be flagged as
invalid, which causes the expanded list state to be updated (and now
all the group positions are invalid, so all expanded groups are
removed from the list of expanded groups). However, this list is
contained via a reference in the saved state bundle, so even though
this list is never explicitly added to this bundle again, in the
onRestoreInstanceState() method the list is empty in the bundle
because the reference was modified.
It kind of looks like a bug to me, but maybe I'm just doing stuff all
wrong. Did anybody get this to work properly? Any suggested
workarounds?
Regards,
Sebastiaan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---