7

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);

}

}

Melanie
  • 3,021
  • 6
  • 38
  • 56
  • I partially solved this by importing android.support.v4.app.LoaderManager. But I'm still getting the same error message on the initLoader call. – Melanie Oct 23 '12 at 21:00

2 Answers2

18

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

Community
  • 1
  • 1
Marcelo
  • 1,471
  • 3
  • 19
  • 22
  • 1
    Thanks very much! I'll have to decide whether to forget the Support Library for now, or dive into Fragments. Dum de dum dum – Melanie Oct 24 '12 at 15:25
  • Can someone explain why Support Library doesn't come with a ListActivity class featuring the getSupportLoaderManager method? Wouldn't that make the use of ListActivity with loaders a lot easier? – user672009 Aug 28 '13 at 23:00
  • I think only Google can answer that. My best guess: they have more important things to do on the Support Library, and implementing a normal activity that handles a ListView is not that hard - ListActivity just make it easier. – Marcelo Sep 04 '13 at 11:15
  • 1
    Thanks very much! I was stuck for the last hour :) – Ektos974 Feb 04 '14 at 17:23
1

LoaderManager cannot be resolved to a type. Can be solved adding the import:

import android.support.v4.app.LoaderManager;

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

Jorgesys
  • 124,308
  • 23
  • 334
  • 268