1

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

}
Community
  • 1
  • 1
Rashid
  • 17
  • 5

1 Answers1

3

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);
    }
});
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
varunkr
  • 5,364
  • 11
  • 50
  • 99
  • It's not working, says cannot resolve symbol 'listview' and 3 more errors – Rashid May 11 '16 at 01:05
  • @Rashid Change listView to the name of the listView that you are using in your app – varunkr May 11 '16 at 01:06
  • 'code'public class MainActivity extends AppCompatActivity{ private List posts; private Toolbar toolbar; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); lv = (ListView) findViewById(R.id.list); setSupportActionBar(toolbar); 'code' – Rashid May 11 '16 at 01:09
  • I did that, but the same error cannot resolve symbol 'setOnItemClickListener' – Rashid May 11 '16 at 01:14
  • Really? lv.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); } }); doesn't work, just paste it !! – varunkr May 11 '16 at 01:18
  • Yes, got same error. Change the code position to just below the onCreate method and got a new error I'm working on. Sorry I'm a newbee. – Rashid May 11 '16 at 01:29
  • If it is a problem simply post your full code after the changes in the question – varunkr May 11 '16 at 01:30
  • Other errors are fixed. A new error at this line Intent intent = new Intent(this, EditNoteActivity.class); it says cannot resolve constructor intent... – Rashid May 11 '16 at 01:34
  • @Rashid Change this to MainActivity.this – varunkr May 11 '16 at 01:35
  • Thanks a lot,Varun. When I moved the code to Oncreate method section it all worked, Why? – Rashid May 11 '16 at 01:42
  • @Rashid It should be in onCreate where was it earlier ? – varunkr May 11 '16 at 01:42
  • You mean outside the functions ? It should be in onCreate or onResume , I think you declared it in the class outside the methods. – varunkr May 11 '16 at 01:44