本文共 2141 字,大约阅读时间需要 7 分钟。
returns its query results to your implementation of
, in the form of a
. In the callback, you can update your data display, do further processing on the
data, and so forth. http://blog.csdn.net/sergeycao
When the loader framework detects changes to data associated with the query, it resets the, closes the current
, and then invokes your implementation of
. Use this callback to delete references to the current
; when the loader framework destroys the
, you won't have outstanding references that cause memory leaks.
The following two snippets are an example of displaying the results of a query, using a backed by a
.
The first snippet shows the and
:
// Gets a handle to the Android built-in ListView widgetmListView = ((ListView) findViewById(android.R.id.list));// Creates a CursorAdaptermAdapter = new SimpleCursorAdapter( this, // Current context R.layout.logitem, // View for each item in the list null, // Don't provide the cursor yet FROM_COLUMNS, // List of cursor columns to display TO_FIELDS, // List of TextViews in each line 0 // flags);// Links the adapter to the ListViewmListView.setAdapter(mAdapter);
The next snippet shows an implementation of that moves the query results in the returned
to the
. Changing the
in the
triggers a refresh of the
with the new data:
public void onLoadFinished(Loaderloader, Cursor cursor){ /* * Move the results into the adapter. This * triggers the ListView to re-display. */ mAdapter.swapCursor(cursor);}
The loader framework resets the whenever the
becomes invalid. This usually occurs because the data associated with the
has changed. Before re-running the query, the framework calls your implementation of
. In this callback, make sure to prevent memory leaks by deleting all references to the current
. Once you return from
, the loader framework re-runs the query.
For example:
public void onLoaderReset(Loaderloader){ // Remove the reference to the current Cursor mAdapter.swapCursor(null);}