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
}
}