我无法从 ListFragment 中的 onListItemClick 开始新的 activity

I can't start new activity from onListItemClick inside ListFragment

我不确定我做错了什么,到目前为止一直在工作 perfectly.Once 我想在选择 item/note 开始新的 activity 时添加新的 activity ] 它不会工作,也不会给我一个错误。

这是ListFragment

public class ListFragmentMain extends ListFragment {

private ArrayList<Note> notes;
private NotesAdapter notesAdapter;


@Override
public void onActivityCreated(Bundle saveInstanceState) {
    super.onActivityCreated(saveInstanceState);
    notes = new ArrayList<>();
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));
    notes.add(new Note("Some Content here","NOTE 1"));

    notesAdapter = new NotesAdapter(getActivity(),notes);
    setListAdapter(notesAdapter);
    getListView().setDivider(ContextCompat.getDrawable(getActivity(),android.R.color.black));
    getListView().setDividerHeight(1);


}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //position of item passed
    Note note = (Note) getListAdapter().getItem(position);
    Intent intent = new Intent(getActivity(),NoteDetailActivity.class);
    //passing info from the item
    intent.putExtra("Title",note.getTitle());
    intent.putExtra("Message",note.getMessage());
    intent.putExtra("NoteID",note.getNoteID());
    startActivity(intent);

}




}

这是我的适配器

public class NotesAdapter extends ArrayAdapter<Note> {

//hold the references of the data
public static class ViewHolder {
    private TextView noteTitle;
    private TextView contentNote;

}



public NotesAdapter(Context context, ArrayList<Note> notes) {
    super(context, 0, notes);
}

@Override
//get called for every row item
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    //data for position
    Note note = getItem(position);
    if (convertView == null) {
        //new object to save view references
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_row, parent, false);

        //getting the references and store in viewHolder
        viewHolder.noteTitle = (TextView) convertView.findViewById(noteTitle);
        viewHolder.contentNote = (TextView) convertView.findViewById(R.id.noteContent);
        //reference holder
        convertView.setTag(viewHolder);
    } else {
        //if there is a view, get the widget for it, from viewHolder
        viewHolder = (ViewHolder) convertView.getTag();
    }
    //filling data with according view
    viewHolder.noteTitle.setText(note.getTitle());
    viewHolder.contentNote.setText(note.getMessage());


    //display the data
    return convertView;
}

}  

最后是注释 class

public class Note {


private String title, message;
private long noteID, dateCreated;



public Note(String message, String title) {

    this.message = message;
    this.title = title;
    this.noteID = 0;
    this.dateCreated = 0;
}

public Note( long dateCreated, String message, long noteID, String title) {

    this.dateCreated = dateCreated;
    this.message = message;
    this.noteID = noteID;
    this.title = title;
}


public long getDateCreated() {
    return dateCreated;
}

public String getMessage() {
    return message;
}

public long getNoteID() {
    return noteID;
}

public String getTitle() {
    return title;
}

public String toString() {
    return "ID: " + noteID + " Title: " + title + " Message " + message  + " Date ";
}

}

这是我的 NoteDetail Activity,当我单击该项目时它应该启动 activity

public class NoteDetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_detail);
}
}

我不确定为什么当我单击 item/note 时,activity 没有启动?而且它根本没有给出任何错误,而且项目正在按我想要的方式呈现。

找到解决方案

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Note note = (Note) getListAdapter().getItem(position);
    Intent intent = new Intent(getActivity(),NoteDetailActivity.class);
    //passing info from the item
    intent.putExtra("Title",note.getTitle());
    intent.putExtra("Message",note.getMessage());
    intent.putExtra("NoteID",note.getNoteID());
    startActivity(intent);

}

您尝试实现此目的的方式已经过时了。出于显而易见的原因,您应该继续使用 RecyclerView。我有一个我开发的应用程序。它 creates/edit 注释,您可以按时间顺序查看它们。同样在点击你被注意到编辑 activity.

此处:https://github.com/muditpant13/Notebook

希望对你有用。 :) 干杯。

我想知道,

尽管 adapter class 中没有实施 getItem(position) 方法,但您为什么要使用 Note note = (Note) getListAdapter().getItem(position);

而不是使用这个,因为我认为你的 note 正在变得 null -

Note note = notes.get(position);