0

I have a listview that extends base adapter. There is a delete button on every list item. When the delete button is clicked then the list item should be deleted. But when I try to do so listview behaves strangely. The delete button is causing the main problem. Sometimes the list is becoming double and sometimes app is crashing. I am trying to add a delete button to the item of the listview. When someone clicks on the button then the list item will be deleted and it will delete the item also from firebase.

I am trying to achieve this. enter image description here

Sorry for my bad English. Advance thanks for your help.

package com.owoshopkeeperpanel.adapters;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.owoshopkeeperpanel.Interface.ItemClickListener;
import com.owoshopkeeperpanel.Model.Cart;
import com.owoshopkeeperpanel.Prevalent.Prevalent;
import com.owoshopkeeperpanel.R;

import java.util.ArrayList;

public class CartListAdapter extends BaseAdapter {
    private Context mCtx;
    private ArrayList<Cart> cartList;

    final DatabaseReference cartListRef = FirebaseDatabase.getInstance().getReference().child("Cart List");

    public CartListAdapter(Context mCtx, ArrayList<Cart> cartList) {
        this.mCtx = mCtx;
        this.cartList = cartList;
    }

    @Override
    public int getCount() {
        return cartList.size();
    }

    @Override
    public Object getItem(int position) {
        return cartList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Cart cart = cartList.get(position);

        if(convertView==null) {
            LayoutInflater layoutInflater = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.cart_items_sample, null);

        }
            ImageView delete = convertView.findViewById(R.id.cart_item_delete);
            ImageView cart_product_image = convertView.findViewById(R.id.cart_product_image);
            TextView cart_product_name = convertView.findViewById(R.id.cart_product_name);
            TextView cart_product_quantity = convertView.findViewById(R.id.cart_product_quantity);
            TextView cart_product_price = convertView.findViewById(R.id.cart_product_price);
            ElegantNumberButton cart_item_change_button = convertView.findViewById(R.id.cart_item_change_btn);

            Glide.with(mCtx).load(cart.getProduct_image()).into(cart_product_image);

            cart_product_name.setText(cart.getProduct_name());
            cart_product_quantity.setText(cart.getProduct_price()+" × "+cart.getNeeded_quantity());
            double product_total_price = Double.parseDouble(cart.getProduct_price()) * Double.parseDouble(cart.getNeeded_quantity());
            cart_product_price.setText("৳ "+String.valueOf(product_total_price));
            cart_item_change_button.setNumber(cart.getNeeded_quantity());

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

                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mCtx);

                    alertDialogBuilder.setMessage("Are you sure you want to remove this item from cart ?");
                    alertDialogBuilder.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    cartListRef.child(Prevalent.currentOnlineUser.getPhone())
                                            .child(String.valueOf(cart.getProduct_id()))
                                            .removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if(task.isSuccessful())
                                            {
                                                Toast.makeText(mCtx, String.valueOf(cartList.size()), Toast.LENGTH_LONG).show();
                                                cartList.remove(cart);
                                                notifyDataSetChanged();
                                            }
                                        }
                                    });
                                }
                            });

                    alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });

                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
                }
            });
        return convertView;
    }
}
  • 2
    Hello. It would be great if you also add crash log too. – Amin Mousavi Aug 31 '20 at 04:25
  • 1
    have you tried this link https://stackoverflow.com/questions/51906903/how-to-delete-data-in-listview-and-also-from-firebase –  Aug 31 '20 at 04:27
  • @AminMousavi It says array index out of Bound. – Amimul Ehsan Rahi Aug 31 '20 at 04:28
  • Consider using `RecyclerView` :) – Anton Potapov Aug 31 '20 at 06:51
  • `String.valueOf(cart.getProduct_id()) Toast your id directly in onClick and see that is the wrong id for that item. You could also toast card.getProductName() to see that it does not match the clicked item. – blackapps Aug 31 '20 at 07:09
  • @AntonPotapov I have to calculate the total price. If I use a recycler view then only the items on the screen will be calculated. Not the whole price. Because the recycler view recycles view. That's why partial total will be calculated. – Amimul Ehsan Rahi Aug 31 '20 at 08:12

0 Answers0