AlertDialog.Builder в пользовательском адаптере представления списка не отображается - PullRequest
2 голосов
/ 25 марта 2020

Привет, ребята, поэтому у меня есть простое X-изображение в конце каждого элемента listView. Мое изображение щелкают, потому что я установил параметр clickable в XML. Но 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);
        }
    });

Ответы [ 2 ]

2 голосов
/ 26 марта 2020

Похоже, отсутствуют две инструкции. 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!

Оповещение диалога официальной документации

2 голосов
/ 26 марта 2020

Я считаю, что только .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();
...