ListView empty if SimpleCursorAdapter closed()

丶灬走出姿态 提交于 2019-12-04 11:33:36
Adil Soomro

Why is this so?

Adapter needs data from provided Cursor and prepare ListView accordingly, and when you call cursor.close() the Cursor is released and made invalid. (means there is no data.)

When and why is necessary to close cursor?

It is necessary to close the Cursor while you are about to leave the Activity and going back from the Activity otherwise Cursor got leaked for that Activity.

It's enough to say cursor.getCount() How come this is allowed?

The structure of Java is based on classes. Every action is done within a specific class. Even echoing "single line" would need a class. and when you are in a class you can access class members with this OR with direct their variables

You should not close your cursor unil your Activity is destroyed (i.e. in onDestroy()) because the implementation of CursorAdapter expects it to be open to be able to requery and filter it.

Since you're calling startManagingCursor anyway, your Activity will automatically deactivite, requery and close the cursor upon the appropriate Activity lifecycle events, so there is no need to close it yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!