Skip to content Skip to sidebar Skip to footer

How To Make Imageview Clickable From Onitemclicklistener?

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(final AdapterView parent, View view, final int position

Solution 1:

If you take image Onclick from adapter,you cannot take setOnItemClickListener from listview event.I suggested you may take Onclick and insteadof setOnItemClickListener take Onclick from adapter.

Solution 2:

First you have to add OnClickListener for Imageview in Adapter class like

viewHolder.button1.setOnClickListener(newOnClickListener() {

@OverridepublicvoidonClick(View v) {
    ((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}

});

Then after you can access ImageView in onItemClick like

@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
    longviewId= view.getId();


if (viewId == R.id.button1) {
        Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
    } elseif (viewId == R.id.button2) {
        Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "ListView clicked" + id, Toast.LENGTH_SHORT).show();
    }
}

Enjoy!...

Solution 3:

It shouldn't be a problem to show a dialog from the adapter. If your adapter needs something that it doesn't have, then you can add a field for your adapter with some listener like private final OnImageClickListener and create an interface:

publicinterfaceOnImageClickListener {
    voidonImageClicked(View view, int position, int id);
}

When you create the adapter from your activity or a fragment, you can implement this interface either in your activity/fragment or inside an anonymous class, and in there you have access to the fragment manager and can do whatever you want.

Post a Comment for "How To Make Imageview Clickable From Onitemclicklistener?"