再次出现InitLoader错误

时间:2013-12-11 19:21:32

标签: java android listview fragment loader

我想为列表视图创建一个加载器但是

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

知道这个错误很常见但是试图导入不同的库仍然出错。如果有人能指出原因,真的很感激。提前谢谢!

我的代码:

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.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 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);
    note_listview_container=(ListView)rootView.findViewById(R.id.note_listview);

    //showNoteList();

    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();
            //String ds = note_database.getData();
            note_database.get_NoteListAdapter();
            note_listview_container.setAdapter(note_database.noteListAdapter);
            getLoaderManager().initLoader(0, null, context);

            note_database.close();
            //note_list.setText(ds);
        }
    });

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

// THE LOADER
class NoteItemsLoader extends AsyncTaskLoader<ArrayList<NoteItems>> {
    private ArrayList<NoteItems> loader_note_items;
    private NoteDatabase loader_db;

    public NoteItemsLoader(Context context, NoteDatabase db) {
        super(context);
        this.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 = loader_db.getNote_items();
        loader_db.close();
        return loader_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) {}

}

0 个答案:

没有答案