Search code examples
androidlistviewappcompatactivity

onlistitemclick in AppCompatActivity


I followed steps mentioned in this answer to set a toolbar for a listview [listview image][1].

Now the list items are not clickable. When using ListActivity list items were clickable, when clicking on any item it will open another activity with item title and its content.

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    Note note = posts.get(position);
    Intent intent = new Intent(this, EditNoteActivity.class);
    intent.putExtra("noteId", note.getId());
    intent.putExtra("noteTitle", note.getTitle());
    intent.putExtra("noteContent", note.getContent());
    startActivity(intent);

}

Solution

  • Do this

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             Note note = posts.get(position);
             Intent intent = new Intent(this, EditNoteActivity.class);
             intent.putExtra("noteId", note.getId());
             intent.putExtra("noteTitle", note.getTitle());
             intent.putExtra("noteContent", note.getContent());
             startActivity(intent);
        }
    });