How open new activity clicking an item in listview?

你离开我真会死。 提交于 2019-11-27 09:14:27

Use This for doing your work

 list.setOnItemClickListener(new AdapterView.onItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
      Intent appInfo = new Intent(YourActivity.this, ApkInfoActivity.class);
       startActivity(appInfo);
   } 
});
Rahul Bhavani
public class MenuYangu extends ListActivity {

String classes[] = { "Quiz Trivia", "Sign A New User", "Friend List",
"Download A File", "Upload A File", "Select Pdf files", "Memory Game",
"Dzidza Maths", "Write Exam" };

@Override
protected void onCreate(Bundle savedInstanceState) 
{
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setListAdapter(new ArrayAdapter<String>(Menuone.this,
   android.R.layout.simple_list_item_1, classes));

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{
     // TODO Auto-generated method stub
     super.onListItemClick(l, v, position, id);

if (position == 0) {
Intent intent = new Intent(this, QuizActivity.class);
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(this, SignUp.class);
startActivity(intent);
}
 else if (position == 2) {
 Intent intent = new Intent(this, FriendList.class);
 startActivity(intent);
 } 
 }

 }

 }

You need to use Intent, You can also pass the clicked listview item data to your new activity.

String classes[] = { "Quiz Trivia", "Sign A New User", "Friend List",
"Download A File", "Upload A File", "Select Pdf files", "Memory Game",
"Dzidza Maths", "Write Exam" };

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                   
Intent intent = new Intent(getApplicationContext(),ApkInfoActivity.class);
                intent.putExtra("name",classes[i]);
                startActivity(intent);


        }
    });

}

Output:

You can find the whole tutorial here

Add setOnItemclickListener() for your Listview.

Use this:

Intent appInfo = new Intent(CurrentActivity.this, ApkInfoActivity.class); startActivity(appInfo);

Try changing the visibility from protected to public for your method header.

Edit

Now that I look at it, your method header is actually wrong. It should be the following:

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)

The variables must be in the same order as they are in the Interface they implement.

for instance if u want to open an activity based on the text u click in listview,ie if "abcd" is the option clicked on the listview and u want to open the activity with the very same name "abcd",then perform this ..

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

    String temp=yourarray[position];


    try{
        Class myclass=Class.forName("yourpackagename."+temp);
        Intent in=new Intent(this,myclass);
        startActivity(in);
        }catch(Exception e){

        }


}

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.

// Add ArrayList and ArrayAdapter:

    final ArrayList<String> listItems = new ArrayList<String>();
        listItems.add("image_one");
        listItems.add("image_two");
        listItems.add("image_three");

    ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<>(this, 
        android.R.layout.simple_list_item_1, listItems);
        myListView.setAdapter(myArrayAdapter);

// Add ArrayList of Classes:

    final ArrayList<Class> intents = new ArrayList<Class>();
        intents.add(image_one.class);
        intents.add(image_two.class);
        intents.add(image_three.class);

// Click on list item to open Class from ArrayList of Classes:

    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
        position, long id) {

            Intent listIntent = new Intent(getApplicationContext(), 
            intents.get(position)); 
            startActivity(listIntent);
        }
    });

SEE IMAGE OF CLASS NAMES HERE

    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();
          }
   }});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!