如何通过点击 android 中的按钮从购物车中删除商品

how to delete an item from shopping cart by button click in android

我可以通过以下代码长按所选项目来从购物清单中删除项目

  listcart.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
           @Override
           public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {


               model=d.get(i);
               final String orderno=model.getOrderno();
               AlertDialog.Builder msg=new AlertDialog.Builder(CartActivity.this)
                       .setTitle("Delete Item")
                       .setMessage("Are you sure delete this item "+model.getItemname())
                       .setIcon(R.drawable.applogo)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           @Override
                           public void onClick(DialogInterface dialogInterface,int i){
                               DBLite db=new DBLite(CartActivity.this);
                               db.DeleteItem(orderno);
                               d=new ArrayList<>(db.GetCart(CartActivity.this));
                               adapterCart=new Cart_Adapter(CartActivity.this,d);
                               listcart.setAdapter(adapterCart);
                               txtOrderTotal.setText(String.valueOf(db.GetSum()));

                           }
                       }).setNegativeButton("No",null);
               msg.create();
               msg.show();
               return false;
           }
    });

我想通过单击 (x) 图片删除此项。我如何为此目的自定义该代码,如下图所示:

在项目布局中创建一个按钮。在您的 Cart_Adapter 适配器 class 中获取它的引用,并在适配器的 getView() 方法中执行此操作(如果它是 BaseAdapter 或 ArrayAdapter)。但如果它是 recyclerview 适配器,那么在 onBindView() 方法里面写这个:

    viewholder.deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // write code here to delete item and refresh adapter
        }
    });

如果已经加载,则无需再次设置适配器。将此代码尝试到 AlertDialog() 的对话框肯定按钮中,它将从适配器 class.

中的 onBindViewHolder() 方法调用
 <yourViewHolderObject>.deleteIcon.setOnClickListener(new AdapterView.OnClickListener() { 
  // like: deleteIcon = view.findViewById(R.id.deleteIcon);
       @Override
       public boolean onClick(View view, int i) {

           model=d.get(i);
           final String orderno=model.getOrderno();
           AlertDialog.Builder msg=new AlertDialog.Builder(CartActivity.this)
                   .setTitle("Delete Item")
                   .setMessage("Are you sure delete this item "+model.getItemname())
                   .setIcon(R.drawable.applogo)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialogInterface,int i){
                           DBLite db=new DBLite(CartActivity.this);
                           db.DeleteItem(orderno);
                           // Also delete this item from your current list
                           listcart.remove(model);
                           d=new ArrayList<>(db.GetCart(CartActivity.this));
                           adapterCart=new Cart_Adapter(CartActivity.this,d);
                           //listcart.setAdapter(adapterCart); // exclude this
                           adapterCart.notifyDataSetChanged(); // include this
                           // this line your reload and update your list
                           txtOrderTotal.setText(String.valueOf(db.GetSum()));

                       }
                   }).setNegativeButton("No",null);
           msg.create();
           msg.show();
           return false;
       }
});

我找到了一种方法并且有效。我通过 cartAdapter activity 处理删除按钮,其中 c=context , ass=arrayList<>

   viewHolder.imgdelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder msg=new AlertDialog.Builder(c)
                    .setTitle("Delete Item")
                    .setMessage("Are you sure delete this item "+data.getItemname())
                    .setIcon(R.drawable.applogo)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface,int i){
                            DBLite db=new DBLite(c);
                            db.DeleteItem(data.getOrderno());
                            ass=new ArrayList<>(db.GetCart(c));
                            adapterCart=new Cart_Adapter(c,ass);
                            listcart.setAdapter((ListAdapter) adapterCart);
                            txtOrderTotal.setText(String.valueOf(db.GetSum()));

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