getLoaderManager.initLoader出现刺激性错误

时间:2013-12-12 04:37:24

标签: java android listview fragment loader

我正在尝试使用带有asynctaskloader的loader来加载我的listview片段,但是在启动加载器时遇到错误:

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

我知道很多人都遇到过这个错误,我确实研究过但仍未解决,无法理解为什么。如果有人有任何想法,请提前回复并提前致谢!

这是我的代码:

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) {}

}

2 个答案:

答案 0 :(得分:2)

替换

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

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

此处您的WhiteNode类实现LoaderManager.LoaderCallbacks,但您的Activity类未实现。所以你必须传递WhiteNote类的引用。

并且一旦检查导入部分为@Praveen Sharma说

答案 1 :(得分:2)

尝试用这些

替换旧的导入
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;

希望这可以解决您的问题。