Hello guys so I have a simple X image at the end of each listView items. My image gets clicked because I set clickable parameter in XML. But the AlertDialog isn't triggered because of the context, I think. I have tried many different ways but nothing works, here is my code:
private int resourceLayout;
private Context mContext;
private List<Product> items;
public ListAdapter(Context context, int resource, List<Product> items) { // ListAdapter constructor
super(context, resource, items);
this.items = items;
this.resourceLayout = resource;
this.mContext = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(mContext);
v = vi.inflate(resourceLayout, null);
}
final Product p = getItem(position);
ImageView deleteImage= (ImageView) v.findViewById(R.id.delete_image);
deleteImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(mContext) // Here is the problem, I think. I tried 4 different ways to get to the MainActivity because there is called my constructor but no succes
.setIcon(R.drawable.del_button)
.setTitle("Are you sure?")
.setMessage("Do you want to delete " + p.getName() + " from you shopping cart ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
items.remove(position);
notifyDataSetChanged();
}
})
.setNegativeButton("No", null);
}
});