自定义列表视图适配器中的AlertDialog.Builder未显示

问题描述 投票:1回答:3

大家好,我在每个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);
        }
    });
android android-alertdialog
3个回答
2
投票

我相信最后只缺少.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();

2
投票

似乎缺少两条说明。 createshow。在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!

Alert dialog official documentation

© www.soinside.com 2019 - 2024. All rights reserved.