ExpandableListView、CursorTreeAdapter 和游标管理
尝试使用 ExpandableListView 和 CursorTreeAdapter。这里引用 Android 手册:
受保护的抽象游标 getChildrenCursor (游标组游标) ... 您有责任在整个 Activity 生命周期中管理此 Cursor。在某些情况下,适配器会自行停用游标,但情况并非总是如此,因此请确保正确管理游标。
好吧,我在Google上找到了一些示例并编写了以下代码:
@Override
protected Cursor getChildrenCursor( Cursor groupCursor ) {
int groupID = groupCursor.getInt( groupIDColumn );
Cursor c = dbAdapter.getHostsCursor( "where expression", groupID );
startManagingCursor( c );
return c;
}
此代码有效,但 ExpandableListView 在每次单击组时都请求新光标以展开它。新游标被创建并添加到活动的托管游标列表中。
但是旧游标会发生什么情况呢?如果我负责根据 Google 手册管理游标,我可以在哪里关闭旧的、未使用的游标,调用 stopManagingCursor() ?如果有人点击展开和折叠组,应用程序会因堆栈溢出或其他系统资源泄漏而崩溃的速度有多快?
请指出正确的方向。
Trying to use ExpandableListView and CursorTreeAdapter. Here th quote from Android manual:
protected abstract Cursor getChildrenCursor (Cursor groupCursor)
...
It is your responsibility to manage this Cursor through the Activity lifecycle. In some situations, the adapter will deactivate the Cursor on its own, but this will not always be the case, so please ensure the Cursor is properly managed.
Ok, I find with Google some examples and wrote the following code:
@Override
protected Cursor getChildrenCursor( Cursor groupCursor ) {
int groupID = groupCursor.getInt( groupIDColumn );
Cursor c = dbAdapter.getHostsCursor( "where expression", groupID );
startManagingCursor( c );
return c;
}
This code works, but ExpandableListView requests new cursor on every click on a group to expand it. New cursors are created and added to Activity's list of managed cursors.
But what happens to the old cursors? Where I can close old, unused cursor, call stopManagingCursor(), if I'm responsible to manage cursors according to Google manual? If someone will be click to expand and collapse groups, how quickly app will crashed with stack overflow or another leak of system resources?
Please point me to the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑这两种选择:
每个组都有一个光标。
没有优化的空间。
选择 1 可能是正确的
Consider these two alternatives:
choice 1 is probably true