How do I get the key \"-KLpcURDV68BcbAvlPFy\" when I know the field \"name\" contains \"efg\" in the following structure in Firebase.
clubs
-KLpcURDV68Bc
We can use HashMap.
initialize:
String for storing Key value. ModelClass for storing you data.
val hashMap: HashMap = hashMapOf()
databaseReference.child("/Product").orderByChild("ProductName")
.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
/*p0.children.mapNotNullTo(models) { it.getValue(ProductModel::class.java) }.run {
}*/
p0.children.forEach {
val value = it.getValue(ProductModel::class.java) as ProductModel
hashMap[it.key!!] = value
}.run { adp.update(hashMap) }
}
})
after this we can also get key & its value by using (index)
5 is Index
hashMap.keys.elementAt(5)
hashMap.values.elementAt(5)