Querying data can be time-consuming, so we want to put the query data in an asynchronous task, query results get Cursor, and then set adapter in the OnPostExecute (Cursor result) method, We may think of using the managedquery of activity to generate cursor so that cursor will be consistent with Acitivity's life cycle, and how perfect the solution is! However, in fact Managedquery also has a lot of limitations, Managedquery generated cursor must ensure that it will not be replaced, because many programs are actually query conditions are uncertain, so we often use the cursor of the new query to replace the original cursor. Therefore, the scope of this method is also very small.
We can't shut down the cursor directly, but note that CursorAdapter does not automatically shut down the cursor at the end of acivity, so you need to turn it off manually in the OnDestroy function.
Copy Code code as follows:
@Override
protected void OnDestroy () {
Super.ondestroy ();
Mphotoloader.stop ();
if (madapter!= null && madapter.getcursor ()!= null) {
Madapter.getcursor (). Close ();
}
}
If cursor is not used in adapter, you can manually turn off cursor.
Copy Code code as follows:
Cursor Cursor = null;
try{
cursor = Mcontext.getcontentresolver (). query (Uri,null,null,null,null);
if (cursor!= null) {
Cursor.movetofirst ();
Do something
}
}catch (Exception e) {
E.printstatcktrace ();
}finally{
if (cursor!= null) {
Cursor.close ();
}
}