Holder.itemView.setOnClickListener onBindViewHolder内部的AlertDialog Builder

时间:2018-12-11 01:54:26

标签: android firebase firebase-realtime-database android-recyclerview android-alertdialog

当用户单击RecyclerView项目时,我正在尝试从数据库中删除项目。“确认对话框”打开,如果用户按“是”,它将从Firebase中删除项目。

我认为问题是由于上下文原因,警报对话框未在应用中显示,因此我应该在上下文中输入什么以在屏幕上显示alertDialog

public void onBindViewHolder(@NonNull final DishViewHolderModify holder, int position) {

    final Dish dish = dishList.get(position);
    holder.textViewDishName.setText(dish.getmName());
    Picasso.get().load(dish.getmImageUrl()).networkPolicy(NetworkPolicy.OFFLINE).into(holder.imageViewDishImage, new Callback() {
        @Override
        public void onSuccess() {
        }
        @Override
        public void onError(Exception e) {
            Picasso.get().load(dish.getmImageUrl()).into(holder.imageViewDishImage);
        }
    });
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Click Happen","Yes");
            final String dishName = holder.textViewDishName.getText().toString();
            new AlertDialog.Builder(v.getContext()) // problem over here
            .setTitle("Delete Dish")
            .setMessage("Are you sure you want to delete")
            .setIcon(R.drawable.ic_delete_forever_black_24dp)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    letsDeleteDish(dishName);
                }
            }).setNegativeButton("No",null);
        }
    });

}

1 个答案:

答案 0 :(得分:1)

我相信您的问题是您没有在show()实例上调用AlertDialog.Builder。我没有发现您的Context参数有问题。

这是构建器的最后一行:

}).setNegativeButton("No",null);

可能看起来应该像这样:

}).setNegativeButton("No",null)
.show();