Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast cursorLoader to Loader<Object>

Hopefully simple java question I've just forgotten or never understood. I've been playing with Loaders in Android 4.0. The program is working as was but now I'm looking to the 'next' part. I want a standard Cursor loader and a custom AsyncTaskLoader. I'm stuck on part 1 trying to convert a cursorLoader to a Loader and return it.

@Override
    //public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    public Loader<Object> onCreateLoader(int id, Bundle args) {

        if (id == LISTVIEWLOADER) {
            String[] projection = { RunnerTable.COLUMN_ID,
                    RunnerTable.COLUMN_NAME };
            CursorLoader cursorLoader = new CursorLoader(getActivity(),
                    FanContentProvider.CONTENT_URI, projection, null, null,
                    null);


            return cursorLoader;   //HERE IS THE PROBLEM
        }
        return null;
    }

Type mismatch cannot convert from CursorLoader to Loader.

I believe can make it work with

public class MainFragment extends ListFragment implements
        //LoaderManager.LoaderCallbacks<Cursor>
        LoaderManager.LoaderCallbacks

But don't like the warning:

LoaderManager.LoaderCallbacks is a raw type. References to generic type LoaderManager.LoaderCallbacks should be parameterized

Thanks for any assistance you can provide.

like image 760
jimsis Avatar asked Oct 24 '12 21:10

jimsis


3 Answers

Even though this is an old thread, it has not yet been fully answered. The issue here was touched on by tch0106, but not fully described.

I just ran into the same issue and resolved it in the imports section. In simple terms: Use the support Loader, CursorLoader and LoaderCallbacks together.

Several classes can be found in both the android local libraries as well as the support libraries. Don't mix the support libraries with the non-support library versions.

// Use these together
import android.support.v4.content.CursorLoader;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;

// Or use these, but don't mix and match
import android.content.CursorLoader;
import android.content.Loader;
import android.app.LoaderManager.LoaderCallbacks;
import android.widget.SimpleCursorAdapter;
like image 191
ILeftItAtHome Avatar answered Nov 20 '22 02:11

ILeftItAtHome


Check your import, you may mis use these 2:

import android.content.CursorLoader;

and

import android.support.v4.content.CursorLoader;
like image 35
tch0106 Avatar answered Nov 20 '22 03:11

tch0106


Change

public Loader<Object> onCreateLoader(int id, Bundle args) {

to

  public Loader<? extends Object> onCreateLoader(int id, Bundle args) {

First statement tells jvm, return type is concrete Object. New statement tells jvm that any TYPE which is of Object.

Please read this tutorial.

like image 2
kosa Avatar answered Nov 20 '22 03:11

kosa