Question


I've got ListFragment and a custom adapter.
Each row in my list consists of two TextViews and one ImageButton. I want to call a method when clicking the Image Button.

public class MyCustomAdapter extends SimpleAdapter {

private final Context context;
private final List<? extends Map<String,? >> data;
private final String[] from;

    public MyCustomAdapter(Context context, List<? extends Map<String, ?>> data, String[] from) {
        super(context, data, R.layout.list_item, from, new int[]{R.id.firstLine, R.id.secondLine});
        this.context=context;
        this.data=data;
        this.from=from;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
        TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
        firstLine.setText((CharSequence) data.get(position).get(from[0]));
        secondLine.setText((CharSequence) data.get(position).get(from[1]));
        ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.tourItemDelete);
        final long tourId = (Long) data.get(position).get("key");
        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //do something dependend on tourId


            }
        });


        return rowView;
    }

I want to have this onClickListener in my ListFragment

The onListItemClick method in my ListFragment is only called when I click on one of the both TextViews but not when I click on the ImageButton.

  1. Is it possible to move this onClickListener from my adapter to the ListFragment? The tourId should also be passed from my adapter to a method in the ListFragment.
  2. Why is the onListItemClick method in my ListFragment not called when I click the ImageButton?

No correct solution

OTHER TIPS

A friend of mine helped me and showed me how to solve my problem.

I created an interface in my adapter and implemented it in my fragment.

public class MyCustomAdapter extends SimpleAdapter {

private OnImgClicked mImageClicked;
private final Context context;
private final List<? extends Map<String,? >> data;
private final String[] from;

public MyCustomAdapter(Context context, List<? extends Map<String, ?>> data, String[] from) {
    super(context, data, R.layout.list_item, from, new int[]{R.id.firstLine, R.id.secondLine});
    this.context=context;
    this.data=data;
    this.from=from;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_item, parent, false);
    TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
    TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
    firstLine.setText((CharSequence) data.get(position).get(from[0]));
    secondLine.setText((CharSequence) data.get(position).get(from[1]));
    ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.tourItemDelete);
    final long tourId = (Long) data.get(position).get("key");
    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mImageClicked.imgClicked(tourId);

        }
    });

    return rowView;
}

public interface OnImgClicked {
    public void imgClicked(long tourId);
}

public void setOnImgClickListener(OnImgClicked imgClicked) {
    this.mImageClicked = imgClicked;
}
}

Implement the interface in the fragment

public class MyFragment extends ListFragment implements MyCustomAdapter.OnImgClicked {
    //...other methods like

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    [...]//init/create variables for MyCustomAdapter Creation
    MyCutsomAdapter adapter = new MyCustomAdapter(getActivity(),....);
    adapter.setOnImgClickListener(this);

    }

    @Override
    public void imgClicked(long tourId) {
        //do something with tourId
    }

}

in getView do

imgBtn.setFocusable(false);
        imgBtn.setClickable(false);  

that should fix ur click problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top