大家好,我在每个listView项的末尾都有一个简单的X图像。我的图片被点击,因为我在XML中设置了clickable参数。但我认为AlertDialog不会因为上下文而触发。我尝试了许多不同的方法,但是没有任何效果,这是我的代码:
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);
}
});
我相信最后只缺少.show()。
AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
.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);
builder.show();
似乎缺少两条说明。 create
和show
。在setNegativeButton
指令的正下方,如下所示添加它们。
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) {
notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.create().show(); //Take a look at these instructions!