Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoaderManager does not accept 'this'

Okay, I surrender. I cannot figure it out.

I'm following a Udacity course on Android Basics and need to figure out how to load data using a Loader. However, when I use the following line, the 'this' is highlighted in red and showing the following error:

Wrong 3rd argument type. Found 'com.example.carl.latestnews.MainActivity', required: 'android.app.LoaderManager.LoaderCallbacks<java.lang.Object>

I've googled, stacked and tried suggestion I've found. I've tried creating an inner class which implements the call backs. I've hit a brickwall and I'm sat here scratching my head trying to figure out what I'm missing!

Can anyone tell me what I'm doing wrong here?

Thanks in advance!

package com.example.carl.latestnews;


 import android.content.Context; 
 import android.os.Bundle; 
 import android.support.v4.app.LoaderManager; 
 import android.support.v4.content.AsyncTaskLoader; 
 import android.support.v4.content.Loader; 
 import android.support.v7.app.AppCompatActivity; 
 import android.view.View; 
 import android.widget.ListView; 
 import android.widget.TextView;
 import java.util.ArrayList;

 public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<ArrayList<ArticleObject>> {

     // ArticleObject is a custom object which contains a headline, date, category etc of a news article

     // URL for Guardian API including API Key
     final static String GUARDIAN_API_URL = "https://content.guardianapis.com/search?";

     // API Key
     final static String GUARDIAN_API_KEY = "test";

     // ID for LoaderManager
     final static int LOADER_MANAGER_ID = 0;


     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         // Get or initialize loader manager
         getLoaderManager().initLoader(LOADER_MANAGER_ID, null, this);

     }

     @Override
     public Loader<ArrayList<ArticleObject>> onCreateLoader(int id, Bundle args) {
         return new dataLoader(); // dataLoader() removed for easy reading
     }

     @Override
     public void onLoadFinished(Loader<ArrayList<ArticleObject>> loader, ArrayList<ArticleObject> data) {
         / UI Update Code
     }

     @Override
     public void onLoaderReset(Loader<ArrayList<ArticleObject>> loader) {
         // Reset Code
     }




 }
like image 589
Caphson Avatar asked Dec 02 '22 14:12

Caphson


2 Answers

Change: getLoaderManager() to getSupportLoaderManager();
Like this:
getLoaderManager().initLoader(ID_FAVORITE_METAINFO_LOADER, args, this);
To
getSupportLoaderManager().initLoader(ID_FAVORITE_METAINFO_LOADER, args, this);

like image 149
兰坡阳 Avatar answered Dec 06 '22 10:12

兰坡阳


That method expects LoaderCallbacks as an argument.

Your Activity needs to implement the LoaderCallbacks interface. OR you provide an anonymous implementation of that interface like:

    LoaderManager.LoaderCallbacks callbacks = new LoaderManager.LoaderCallbacks() {
        @Override
        public Loader onCreateLoader(int id, Bundle args) {
            return null;
        }

        @Override
        public void onLoadFinished(Loader loader, Object data) {

        }

        @Override
        public void onLoaderReset(Loader loader) {

        }

  getLoaderManager().initLoader(LOADER_MANAGER_ID, null, callback);

Implementation of the interface methods is up to you though, this code won't work right away.

like image 42
fweigl Avatar answered Dec 06 '22 11:12

fweigl