Open different activities on each listview click

后端 未结 4 1071
春和景丽
春和景丽 2021-01-27 07:18

I am having problem setting OnClickListener to my listview. I want to open different activities when different listview item is being clicked. But i am not able to get it done.

4条回答
  •  迷失自我
    2021-01-27 07:39

    You have to use OnItemClickListener for it.

     lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position,
                        long id) {
                 Intent intent = null;
                  switch(position){
                          case 0 :
                           intent = new Intent(HomeActivity.this,SecondActivity.class);
                           break;
                          case 1 :
                           intent = new Intent(HomeActivity.this,ThirdActivity.class);
                           break;
    .......
    }
    
    startActivity(intent);
                }
            });
    

提交回复
热议问题