error OnListItemClick and setListAdapter is undefined for the type fragment

夙愿已清 提交于 2019-12-14 03:15:10

问题


i have error in onListItemClick error message is "The method onListItemClick(ListView, View, int, long) is undefined for the type Fragment" and setListAdapter error message is "The method setListAdapter(SimpleCursorAdapter) is undefined for the type ReviewFragment", this my full code,

import android.app.Fragment;
import android.os.Bundle;

import android.support.v4.widget.SimpleCursorAdapter;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ReviewFragment extends Fragment {

private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;

private static final int DELETE_ID = Menu.FIRST;
private int mNoteNumber = 1;

private NotesDbAdapter mDbHelper;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_review, container, false);

    mDbHelper = new NotesDbAdapter (getActivity());
    mDbHelper.open();
    fillData();

    registerForContextMenu(getView());
    Button addnote = (Button) v.findViewById(R.id.addnotebutton);
    addnote.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View v) {
            createNote();
            }
    });return v;
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater mInflater = getActivity().getMenuInflater();
    mInflater.inflate(R.menu.notelist_menu, menu);

}

private void createNote() {
    Intent i = new Intent(getActivity(), NoteEdit.class);
    startActivityForResult(i, ACTIVITY_CREATE);     
}


public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(getActivity(), NoteEdit.class);
    i.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}


@SuppressWarnings("deprecation")
private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    getActivity().startManagingCursor(notesCursor);

    String[] from = new String[] { NotesDbAdapter.KEY_TITLE ,NotesDbAdapter.KEY_DATE};
    int[] to = new int[] { R.id.text1 ,R.id.date_row};

    SimpleCursorAdapter notes =
            new SimpleCursorAdapter(getActivity(), R.layout.note_row, notesCursor, from, to);
        setListAdapter(notes);
    }
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteNote(info.id);
            fillData();
            return true;
    }
    return super.onContextItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();

}  
}

来源:https://stackoverflow.com/questions/24723215/error-onlistitemclick-and-setlistadapter-is-undefined-for-the-type-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!