gpt4 book ai didi

java - getLoaderManager.initLoader 出现恼人的错误

转载 作者:行者123 更新时间:2023-11-29 03:26:58 26 4
gpt4 key购买 nike

我正在尝试使用带有 asynctaskloader 的加载器将我的 ListView 加载到 fragment 上,但在启动加载器时出错:

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

}

最佳答案

替换

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

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

这里您的 WhiteNode 类实现了 LoaderManager.LoaderCallbacks 但您的 Activity 类没有实现。所以你必须传递 WhiteNote 类的引用。

并按照@Praveen Sharma 的说法检查进口部分

关于java - getLoaderManager.initLoader 出现恼人的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20535136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com