我无法从ListFragment中的onListItemClick启动新活动

时间:2017-03-24 09:08:00

标签: java android android-fragments android-activity android-listfragment

我不确定我做错了什么,到目前为止工作得很完美。一旦我选择了项目/备注来开始一项新活动,我想添加新活动,它将无效,也不会给我一个错误。

这是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;
}

}  

最后是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);
}
}

我不确定为什么当我点击一个项目/注释时,没有开始活动?而且它根本没有给出任何错误,而且,这些项目都是我想要的。

3 个答案:

答案 0 :(得分:0)

您尝试实现此目标的方式已经过时了。出于显而易见的原因,您应该继续使用RecyclerView。我有一个应用程序,我开发了一些时间。它可以创建/编辑注释,您可以按时间顺序查看它们。此外,点击您的注册编辑活动。

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

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

答案 1 :(得分:0)

我想知道,

尽管您的<link href="https://fonts.googleapis.com/css?family=Roboto:700" rel="stylesheet"> 课程中没有实施.text { font-family: 'Roboto', sans-serif; color: white; font-size: large; } 方法,但为什么使用Note note = (Note) getListAdapter().getItem(position);

而是使用此功能,因为我认为您的getItem(position)正在adapter -

note

答案 2 :(得分:-1)

找到解决方案

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

}