I'm new to android development and im having a bit of problem with firebase database. I'm creating a app somewhat similar to e-commerce application.
category1
child1
name: nameOfChild1
value: value
child2
name: nameOfChild2
value: value
child3
This is how my database is structured. im using
dbref = FirebaseDatabase.getInstance().getReference("requiredCategory")
dbref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (productSnapshot in snapshot.children) {
val product = productSnapshot.getValue(Product::class.java)
productlist.add(product!!)
}
productrecyclerview.adapter = productAdapter(productlist)
}
}
And Product
class is
data class Product(
var id: String? = null,
var price: String? = null,
)
Instead i would like to change my structure to
category1
child1
nameOfNested1: value
nameOfNested2: value
child2
nameOfNested1: value
nameOfNested2: value
child3
nameOfNested1: Value
nameOfNested2: value
category2
child1
child2
child3
I want to retrive both the key: nameOfNested
and value:value
.How do i go on and change the code get both the id and value?
Thanks in Advance
CodePudding user response:
If you also want to get the key of each product, you can do so with:
for (productSnapshot in snapshot.children) {
val key = productSnapshot.key //