Search code examples
javaandroidandroid-fragmentsandroid-activityandroid-listfragment

I can't start new activity from onListItemClick inside ListFragment


I am not sure what i am doing wrong, so far was working perfectly.Once i wanted to add new activity when item/note is selected to start a new activity it won't work nor gives me an error.

This is 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);

}




}

And this is my Adapter

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

}  

And finally the Note 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 ";
}

}

And this is my NoteDetail Activity, when i click the item it should start this activity

public class NoteDetailActivity extends AppCompatActivity {

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

I am not sure why when i click an item/note, doesn't start the activity? And it is not giving any error at all, and, the items are rendering as i want.


Solution

  • I wonder,

    Why are you using Note note = (Note) getListAdapter().getItem(position); in spite of having no getItem(position) method implemented in your adapter class?

    Instead use this, as I think your note is getting null -

    Note note = notes.get(position);