Deleting an item from listView leaves the list empty

Viewed 25

I am developing an app for a bar, which has a shopping cart that loads into a listView. Each item in the Listview has a button that allows you to remove it. My problem is that when I delete an item, all the others disappear, having to refresh the listView again so that the list is shown without the deleted item. The method I used to load the listView is as follows: the snippet calls a repository that fetches the information from Firebase. Being asynchronous, I used an interface to receive the data, and then send the received list to the adapter. I share the code to see if someone knows how to guide me.

FragmentCarrito

class FragmentCarrito : Fragment(R.layout.fragment_carrito), CarritoCallBack {

    private lateinit var binding: FragmentCarritoBinding
    private lateinit var adapter: CarritoAdapter
    private lateinit var mDatabaseReference: DatabaseReference
    var edit = false


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding= FragmentCarritoBinding.inflate(layoutInflater)
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mDatabaseReference= FirebaseDatabase.getInstance().reference

        cargarLista(false, view)
        binding.btnEditar.setOnClickListener {
            if (edit){
                cargarLista(false,view)
                edit = false
            }else{
                cargarLista(true,view)
                edit = true
            }
        }

        binding.btnContinuar.setOnClickListener {
            ClicksCarrito(it).ContinuarAFinalizarPedido()
        }
    }

     fun cargarLista(edit:Boolean, view: View){
        var lista  = mutableListOf<ItemCarrito>()
        RepositorioCarrito(view.context).recuperarCarrito(object: CarritoCallBack {
            override fun onCallback(list: MutableList<ItemCarrito>) {
                super.onCallback(list)
                lista.clear()
                lista= list
                 var subtotal =0
                for(item in lista){
                    subtotal += item.precio.toString().toInt()
                }
                binding.tvSubtotal.text="$$subtotal"
                adapter= CarritoAdapter(view.context,lista,edit,mDatabaseReference)
                binding.listViewCarrito.adapter= adapter

            }

        })
    }


}

Adapter Carrito

class CarritoAdapter(
    context1: Context?,
    var lista: MutableList<ItemCarrito>,
    val edit: Boolean,
    val reference: DatabaseReference?
) : ArrayAdapter<ItemCarrito>(context1!!, R.layout.item_carrito, lista) {
    private lateinit var binding: ItemCarritoBinding
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val inflater: LayoutInflater = LayoutInflater.from(context)

        binding = ItemCarritoBinding.inflate(inflater)
        binding.tvCantidad.text = lista[position].cantidad.toString()
        binding.tvTitulo.text = lista[position].titulo.toString()
        binding.tvPrecio.text = "$${lista[position].precio.toString()}"
        binding.tvNota.text = lista[position].nota.toString()
        if (edit) {
            binding.ibEliminar.visibility = View.VISIBLE
        } else {
            binding.ibEliminar.visibility = View.GONE
        }


        binding.ibEliminar.setOnClickListener {
            reference?.child("Usuarios")
                ?.child(FirebaseAuth.getInstance().currentUser?.uid.toString())?.child("Carrito")
                ?.child(lista[position].titulo.toString())?.removeValue()
        }

        return binding.root
    }


}

Repository

class RepositorioCarrito(var context: Context) {


     fun recuperarCarrito(myCallback: CarritoCallBack){
        val mutablelist = mutableListOf<ItemCarrito>()
        val database = Firebase.database
        val ref = database.reference.child("Usuarios").child(FirebaseAuth.getInstance().currentUser?.uid.toString())
            .child("Carrito")

        try {
            ref.addValueEventListener(object : ValueEventListener {
                override fun onDataChange(snapshot: DataSnapshot) {
                    for (producto in snapshot.children) {
                        var prod: ItemCarrito = producto.getValue<ItemCarrito>()!!
                        mutablelist.add(prod)
                    }
                    myCallback.onCallback(mutablelist)
                }

                override fun onCancelled(error: DatabaseError) {
                }
            })


        }
        catch (e:Exception){
            Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show()
        }

                }
}

Interface

 interface CarritoCallBack {
      public fun onCallback(list:MutableList<ItemCarrito>){
     }
}
0 Answers
Related