Search code examples
androidandroid-fragmentsandroid-loadermanagerandroid-loaderasynctaskloader

AsyncTaskLoader in fragment


I have a simple fragment, trying to implement AsyncTaskLoader in it. But i am getting one compile time error :

error: incompatible types required: Loader< List< String>> found: LoaderDrone

the error is in the onCreateLoader method. what am i missing ?

After some research i am unable to find the solution.

here is the code

 public class SubPlaceFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<String>> {


 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getLoaderManager().initLoader(0, null, this);
        getLoaderManager().getLoader(0).startLoading();
    }


  @Override
        public Loader<List<String>> onCreateLoader(int id, Bundle args) {
            return new LoaderDrone(getActivity());
        }

        @Override
        public void onLoadFinished(Loader<List<String>> loader, List<String> data) {

        }

        @Override
        public void onLoaderReset(Loader<List<String>> loader) {

        }

        public static class LoaderDrone extends AsyncTaskLoader<List<String>> {

            public LoaderDrone(Context context) {
                super(context);
                onForceLoad();
            }

            @Override
            public List<String> loadInBackground() {
                List<String> results = null;


                return results;
            }
        }
    }

Thanks for the help :)


Solution

  • Make sure you are importing the correct Loader class.

    If you're using a support Fragment (android.support.v4.app.Fragment), you need to use a support Loader (android.support.v4.content.Loader).

    If you're using native fragments (android.app.Fragment), then you need to use the native Loaders (android.content.Loader).