Error in LoaderManager when using support library

I am trying to convert my code using cursors to use CursorLoaders. To support older versions of Android, I installed the support library and instead of importing android.content.CursorLoader, I import android.support.v4.content.CursorLoader. Now code that compiled perfectly throws two errors. The first error on the LoaderManager is when it is implemented. Error: LoaderManager could not be allowed for the type. The second error is calling initLoader. Error: initLoader (int, Bundle, LoaderManager.LoaderCallbacks) method in LoaderManager type is not applicable for arguments (int, Bundle, MyList). Here is 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); } 

}

+6
source share
2 answers

To return android.support.v4.app.LoaderManager , you need to use getSupportLoaderManager () . Unfortunately, this method is not available for ListActivity . This may help you with this: How to load LoaderManager into ListActivity

+18
source

LoaderManager cannot be allowed for a type . You can decide to add import:

 import android.support.v4.app.LoaderManager; 

and as Marcelo points out, use getSupportLoaderManager() instead of getLoaderManager()

+1
source

All Articles