Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make more than one listview respond for different onItemClickListener?

Tags:

java

android

I'm using 4list views with class extending Activity. I have to perform different list item click function. Can i use the following method code for it? So how to set a single onItemClickListener for multiple ListViews?

   public void onItemClick(AdapterView<?> adapter, View view, int index, long id)
   { 
       switch(view.getId())
       {
          case <listview1 Id> : //call method 1; break;

          case <listview2 Id> : //call method 2; break;

          case <listview3 Id> : //call method 3; break;
       }
   }
like image 984
Pattabi Raman Avatar asked Feb 22 '23 20:02

Pattabi Raman


1 Answers

Set different adapters for different list views. I take two list view for example. So take two adapter for these two. And setOnItemClick(context) will be like as below..

 public void onItemClick(AdapterView<?> adv, View v, int arg2, long arg3) {
    // TODO Auto-generated method stub

    switch(adv.getId()) {
        case R.id.ListView1:
            Toast.makeText(this, "list1", Toast.LENGTH_LONG).show();
            break;
        case R.id.ListView2:
            Toast.makeText(this, "list2", Toast.LENGTH_LONG).show();
            break;
    }

}
like image 65
Debarati Avatar answered Feb 25 '23 12:02

Debarati