Error on LoaderManager when using Support Library
Asked Answered
L

2

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

}

}

Leenaleeper answered 23/10, 2012 at 20:38 Comment(1)
I partially solved this by importing android.support.v4.app.LoaderManager. But I'm still getting the same error message on the initLoader call.Leenaleeper
A
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

Arnie answered 23/10, 2012 at 21:49 Comment(3)
Thanks very much! I'll have to decide whether to forget the Support Library for now, or dive into Fragments. Dum de dum dumLeenaleeper
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?Lexicologist
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.Arnie
S
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()

Subtend answered 26/11, 2014 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.