How open new activity clicking an item in listview?

前端 未结 10 2097
我在风中等你
我在风中等你 2020-11-30 10:17

I can\'t start a new activity clicking over an item in my listview. I want that onItemClick can open the ApkInfoActivity.. Actually when i click no

10条回答
  •  误落风尘
    2020-11-30 10:46

    Giving an explanation to my answer. I assume that you have set your listview in order just as in your posted code. I will only review this part of your code: super.onListItemClick(l, v, position, id); I don't this is necessary. In the case of the example I gave:

    lv.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView adapter, View view, int position, long arg) {
          if(position==0){
         Intent appInfo = new Intent(SwahiliService.this, DisplayActivity.class);
          startActivity(appInfo);
      } 
          if(position==1){
              Intent english=new Intent(SwahiliService.this,EnglishService.class);
              startActivity(english);
          }
          if(position==2){
              Toast.makeText(getApplicationContext(),"You have selected pst3", Toast.LENGTH_LONG).show();
          }
    

    I am just setting a lister to my listview which I have called lv, my adapter(which is the holder of my listview items) sets three variables, a View, int for position and long for argument:, I refer to the item selected on listview by its position which as usual starts at 0 (though you can instantiate it to start at any other number as you wish e,g int position=1, starts the item count at 1). From here you can then use any control struct to start activity as per item clicked, in my case, I used a for loop since I assumed my listview has three items only, for larger listview items, you can use a for-loop. Please note how I start my new activity by first referencing to current activity as follows (SwahiliService.this) of which can safely be replace by (this keyword only) and then follows the activity I want to start. I hope this is now more elaborate.

提交回复
热议问题