我无法从 ListFragment 内的 onListItemClick 开始新活动
Posted
技术标签:
【中文标题】我无法从 ListFragment 内的 onListItemClick 开始新活动【英文标题】:I can't start new activity from onListItemClick inside ListFragment 【发布时间】:2017-08-17 03:31:38 【问题描述】:我不确定我做错了什么,到目前为止工作正常。一旦我想在选择项目/笔记开始新活动时添加新活动,它不会工作也不会给我一个错误。
这是列表片段
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;
最后是 Note 类
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 活动,当我单击该项目时,它应该启动此活动
public class NoteDetailActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_detail);
我不知道为什么当我点击一个项目/笔记时,没有启动活动?而且它根本没有给出任何错误,并且项目正在按我想要的方式呈现。
【问题讨论】:
您是否在 AndroidManifest.xml 中声明了此活动 Intent intent = new Intent(getActivity().getApplicationContext(), NoteDetailActivity.class); 是的,一切都在那里 去掉`super.onListItemClick(l, v, position, id);` 你能把你的日志也发一下吗 【参考方案1】:我想知道,
尽管在您的adapter
类中没有实现getItem(position)
方法,您为什么还要使用Note note = (Note) getListAdapter().getItem(position);
?
改用这个,因为我认为你的 note
正在获得 null
-
Note note = notes.get(position);
【讨论】:
【参考方案2】:您试图实现这一目标的方式已经过时了。出于明显的原因,您应该继续使用 RecyclerView。我有一个我曾经开发过的应用程序。它创建/编辑笔记,您可以按时间顺序查看它们。同样在点击您的时候会注意到编辑活动。
这里:https://github.com/muditpant13/Notebook
希望对你有用。 :) 干杯。
【讨论】:
【参考方案3】:找到解决办法
@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);
【讨论】:
以上是关于我无法从 ListFragment 内的 onListItemClick 开始新活动的主要内容,如果未能解决你的问题,请参考以下文章
无法覆盖 ListFragment 中的 onCreateOptionsMenu
在 ListFragment 中无法识别 findViewById [重复]
Android:无法让 Checkbox OnClickListener 在 ListFragment 中工作
Android Studio 中的 ListFragment