0
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) 
        {

         // My code is here. I want make my image clickable. I don't want make it clickable in my CustomAdapter getView() method because it doesn't allows me open DialogFragment from there.
         // I already setted in xml of image clickable="true" and focusable = "false"
         //I'll admit for any help and thanks in advance

        }
}
pleft
  • 7,567
  • 2
  • 21
  • 45
Shai Shamai
  • 21
  • 1
  • 7

3 Answers3

3

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.

Vadivel
  • 780
  • 1
  • 6
  • 21
  • Yes but if i'm taking onClick from my adapter into getView() method i can't get there fragment manager for opening DialogFragment – Shai Shamai Nov 18 '16 at 09:08
  • Put toast code on onclick() method ,check weather is it work or not,if it work then the problem is dialog fragment – Vadivel Nov 18 '16 at 09:11
1

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

viewHolder.button1.setOnClickListener(new OnClickListener() {

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

});

Then after you can access ImageView in onItemClick like

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


if (viewId == R.id.button1) {
        Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
    } else if (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!...

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
  • I'm clicking for up vote button but it doesn't change vote number can't understand why. – Shai Shamai Nov 18 '16 at 09:33
  • Mehul can you explain why you are passing the following three paramiters into ((ListView) parent).performItemClick(v, position, 0); if i understand clear parent is adapter of listView, V is clicked view in my case image, position is position of image into raw and why ZERO? – Shai Shamai Nov 18 '16 at 09:40
  • go with this Link :https://developer.android.com/reference/android/widget/AdapterView.html#performItemClick(android.view.View, int, long) – Mehul Kabaria Nov 18 '16 at 09:46
0

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:

public interface OnImageClickListener {
    void onImageClicked(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.

Malcolm
  • 41,014
  • 11
  • 68
  • 91