I'm trying to convert my code from using cursors to using CursorLoaders. In order to support older versions of Android, I've installed the Support Library and instead of importing android.content.CursorLoader, I'm importing android.support.v4.content.CursorLoader. Now code that compiled fine is throwing two errors. The first error is on LoaderManager when I implement it. The error is: LoaderManager cannot be resolved to a type. The second error is on the call to initLoader. The error is: The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, Bundle, MyList). Here's my code:
public class MyList extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String[] PROJECTION = new String[] { "_id", "fieldname" };
private static final int LOADER_ID = 0;
private static final String MYTABLE_BASEPATH = "MyTable_tbl";
private static final String AUTHORITY = "SQLData";
public static final Uri MY_URI = Uri.parse("content://" + AUTHORITY + "/" + MYTABLE_BASEPATH);
private SimpleCursorAdapter mAdapter;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent myData = getIntent();
Bundle info = myData.getExtras();
if (info != null){
Cursor c;
String[] dataColumns = { "fieldname" };
int[] viewIDs = { R.id.mylist1 };
SimpleCursorAdapter adapter;
adapter = new SimpleCursorAdapter(this, R.layout.mylist, null, dataColumns, viewIDs, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
getLoaderManager().initLoader(0, info, this);
}
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = "level = '" + args.getString("Level") + "'";
return new CursorLoader(this, MY_URI,
PROJECTION, selection, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()) {
case LOADER_ID:
mAdapter.swapCursor(cursor);
break;
}
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
In order to return an android.support.v4.app.LoaderManager
, you need to use getSupportLoaderManager() .
Unfortunately, this method is not available to ListActivity
.
This might help you with that:
How to get LoaderManager in a ListActivity