Java 我能';t从onListItem开始新活动单击ListFragment内部
我不确定我做错了什么,到目前为止工作得很好。一旦我想添加新活动,当选择item/note启动新活动时,它将不起作用,也不会给我一个错误 这是ListFragmentJava 我能';t从onListItem开始新活动单击ListFragment内部,java,android,android-fragments,android-activity,android-listfragment,Java,Android,Android Fragments,Android Activity,Android Listfragment,我不确定我做错了什么,到目前为止工作得很好。一旦我想添加新活动,当选择item/note启动新活动时,它将不起作用,也不会给我一个错误 这是ListFragment public class ListFragmentMain extends ListFragment { private ArrayList<Note> notes; private NotesAdapter notesAdapter; @Override public void onActivityCreated
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);
}
}
}
这是我的NoteDetail活动,当我点击这个项目时,它应该开始这个活动
public class NoteDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_detail);
}
}
我不知道为什么当我单击一个项目/注释时,没有启动活动?而且它根本没有给出任何错误,而且,这些项目正按照我的要求呈现。找到解决方案
@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,原因很明显。我有一个我以前开发的应用程序。它创建/编辑注释,您可以按时间顺序查看它们。此外,单击“编辑”按钮可记录编辑活动 在这里: 希望对您有用。:)干杯。我想知道, 为什么要使用
Note Note=(Note)getListAdapter().getItem(position)代码>尽管在适配器类中没有实现getItem(position)
方法?
相反,请使用此选项,因为我认为您的注释
将获得null
-
Note note = notes.get(position);
您是否在AndroidManifest.xmlIntent intent=new intent(getActivity().getApplicationContext(),NoteDetailActivity.class)上声明了此活动;是的,一切都在那里删除'super.onListItemClick(l,v,position,id)`你也可以发布你的日志吗
@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);
}
Note note = notes.get(position);