I am trying to find some of the total prices from my collects but it seems there is a data mismatch somewhere I can't solve so if anyone can assist me to do so I will be thankful
val id = auth.currentUser?.uid.toString()
dref = FirebaseDatabase.getInstance().getReference("tempCart").child(id)
dref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if(snapshot.exists()){
for (cartSnapshot in snapshot.children){
val cart = cartSnapshot.getValue(CartData::class.java)
cartList.add(cart!!)
toto = cartSnapshot.child("totalPrice").getValue(Int::class.java)!!
toto = toto
}
orderCost.text = toto.toString()
cartRecycler.adapter = cartAdapter
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
I have tried several times to fix it by reading the guides online they are changing the data from firebase to integer, but when I do so it tells me I cant cast java.lang.string
to int
CodePudding user response:
Your total price may be a numeric value, but you stored it as a string in your database. So you will have to read that string value from the database, and then convert it to a number:
val str = cartSnapshot.child("totalPrice").getValue(String::class.java)!!
val value = str.toInt();
toto = value
I strongly recommend storing the totalPrice
as a number in the database though, as its best to fix this type of type conversion problem at the source.