I'm trying to use loader with asynctaskloader to load my listview on a fragment but got error with starting the loader:

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, null, context)

I know many have encounterd this error and i did researched but still not solved, can't understand why. If anyone got any ideas please answwer and thanks in advance!

here's my code:

import java.util.ArrayList;

import com.bblackbb.jotdownv2.R;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.content.AsyncTaskLoader;
import android.content.Loader;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.app.Activity;

public class WhiteNote extends Fragment implements **strong text**LoaderManager.LoaderCallbacks<ArrayList<NoteItems>> {
private NoteDatabase note_database;
private int i=0;
public Context context;
public NoteListAdapter noteListAdapter;
public ListView note_listview_container;
public SQLiteDatabase note_sqldb;
public Cursor c;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.white_note, container, false);

    context=getActivity();
    note_database = new NoteDatabase(context);

    final EditText text_ed_1;
    final EditText text_ed_2;
    Button button_addNote;
    Button button_listallNote;
    Button button_delallNote;

    text_ed_1 = (EditText)rootView.findViewById(R.id.textedit1);
    text_ed_2 = (EditText)rootView.findViewById(R.id.textedit2);
    button_addNote = (Button)rootView.findViewById(R.id.button1);
    button_listallNote = (Button)rootView.findViewById(R.id.button2);
    button_delallNote = (Button)rootView.findViewById(R.id.button3);

    button_addNote.setOnClickListener(new OnClickListener() {     
        @Override
        public void onClick(View v) {
            note_database.open();

            note_database.createData(text_ed_1.getText().toString(),text_ed_2.getText().toString());
            //i++;
            note_database.close();
        }
    });

    button_listallNote.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            note_database.open();

            note_database.get_NoteListAdapter();
            note_listview_container.setAdapter(note_database.noteListAdapter);
            getLoaderManager().initLoader(0, null,context);

            note_database.close();
        }
    });

    button_delallNote.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            note_database.open();
            note_database.deleteAllNote();
            note_database.close();
        }
    });
    return rootView;
}

@Override
public Loader<ArrayList<NoteItems>> onCreateLoader(int id, Bundle args) {
    return new NoteItemsLoader(context, note_database);
}

@Override
public void onLoadFinished(Loader<ArrayList<NoteItems>> loader,
                       ArrayList<NoteItems> data) {

    note_listview_container.setAdapter(new NoteListAdapter(context,data));

}

@Override
public void onLoaderReset(Loader<ArrayList<NoteItems>> loader) {
    note_listview_container.setAdapter(null);
}
}


class NoteItemsLoader extends AsyncTaskLoader<ArrayList<NoteItems>> {
    private ArrayList<NoteItems> loader_note_items= new ArrayList<NoteItems>();
    private NoteDatabase loader_db;

    public NoteItemsLoader(Context context, NoteDatabase db) {
        super(context);
        loader_db = db;
    }

    @Override
    protected void onStartLoading() {
        if (loader_note_items != null) {
            deliverResult(loader_note_items); // Use the cache
        }
        else
            forceLoad();
    }

    @Override
    protected void onStopLoading() {
        cancelLoad();
    }

    @Override
    public ArrayList<NoteItems> loadInBackground() {              
        loader_db.open();  // Query the database

        ArrayList<NoteItems> note_items = new ArrayList<NoteItems>();
        loader_db.get_NoteListLoader(note_items,loader_db);

        loader_db.close();
        return note_items;
    }

    @Override
    public void deliverResult(ArrayList<NoteItems> data) {
        loader_note_items = data; // Caching
        super.deliverResult(data);
    }

    @Override
    protected void onReset() {
        super.onReset();
        onStopLoading();
        loader_note_items = null;
    }

    @Override
    public void onCanceled(ArrayList<NoteItems> data) {
         super.onCanceled(data);
         loader_note_items = null;
    }

    protected void onReleaseResources(ArrayList<NoteItems> data) {}

}
有帮助吗?

解决方案

replace

  getLoaderManager().initLoader(0, null,context);

with

  getLoaderManager().initLoader(0, null,WhiteNote.this);

here your WhiteNode class implements LoaderManager.LoaderCallbacks but your Activity class in not implementing. so you have to pass reference of WhiteNote class.

and once check imports section as @Praveen Sharma said

其他提示

Try to replace your old imports with these

import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;

hope this will fix your problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top