Skip to content Skip to sidebar Skip to footer

How To Set Onitemclick In Custom Listview

I created a listview using listview adapter that extends base adapter. It works fine. but I need to add onitemClick Listener to listview, when I click a list item it go to another

Solution 1:

along with the listitem

if you want to get some data from the ListItem, just use parent.getItemAtPosition(position) inside the listener which was already mentioned. This will return a corresponding Object that you can perform further actions with

   HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position);
  String one = map.get("key");
  String two = map.get("key2");

  Intent next = new Intent(FirstActivity.this, NextActivity.class);
  next.putExtra("One", one);
  next.putExtra("Two", two);
  startActivity(next);

Get it in your second Activity:

Bundleextras= getIntent().getExtras();
 Stringone= extras.getStringExtra("One");
 Stringtwo= extras.getStringExtra("Two");

Solution 2:

Use setOnItemClickListener:

lview.setOnItemClickListener(new OnItemClickListener() {

   publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {

       Intent intent = new Intent(Activity.this, secondActivity.class);
    startActivity(intent);
   }
 });

Solution 3:

Try this:

list_view.setOnItemClickListener(new OnItemClickListener() {

publicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
        {

         Intent myIntent = new Intent(Current_Activity.this,Next_Activity.class);
         startActivity(myIntent);
}
});

Solution 4:

Create your onItemClickListener in your listview like this:

lview.setOnItemClickListener(newOnItemClickListener() {

            @OverridepublicvoidonItemClick(AdapterView parent, View view,
                    int position, long id) {
     Intentintent=newintent(mainactivity.this, target.class);
     // maybe put some extra arguments to the intent 
     startActivity(intent);

            }
        });

To put your extra data to the next activity use your intent: Intent doc

Post a Comment for "How To Set Onitemclick In Custom Listview"