Java – loadermanager does not accept ‘this’
OK, I surrendered. I don't understand
I'm paying attention to the udacity course of Android basics and need to figure out how to use loader to load data. However, when I use the following line, "this" is highlighted in red and displays the following error:
Wrong 3rd argument type. Found 'com.example.carl.latestnews.MainActivity', required: 'android.app.LoaderManager.LoaderCallbacks<java.lang.Object>
I used Google search, piled up and tried the suggestions I found. I've tried to create an internal class that implements callbacks. I hit a brick wall, and I sat here scratching my head to find out what I missed!
Who can tell me what I did wrong here?
Thank you 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 onl oadFinished(Loader<ArrayList<ArticleObject>> loader, ArrayList<ArticleObject> data) {
/ UI Update Code
}
@Override
public void onl oaderReset(Loader<ArrayList<ArticleObject>> loader) {
// Reset Code
}
}
resolvent:
The method expects loadercallbacks as a parameter
Your activity needs to implement the loadercallbacks interface. Or you can provide an anonymous implementation of the interface, such as:
LoaderManager.LoaderCallbacks callbacks = new LoaderManager.LoaderCallbacks() {
@Override
public Loader onCreateLoader(int id, Bundle args) {
return null;
}
@Override
public void onl oadFinished(Loader loader, Object data) {
}
@Override
public void onl oaderReset(Loader loader) {
}
getLoaderManager().initLoader(LOADER_MANAGER_ID, null, callback);
The implementation of interface methods is up to you, but this code will not take effect immediately