How do I get the key \"-KLpcURDV68BcbAvlPFy\" when I know the field \"name\" contains \"efg\" in the following structure in Firebase.
clubs
-KLpcURDV68Bc
If anyone needs to do this using Kotlin:
mDatabase.child("clubs")
.orderByChild("name")
.equalTo("efg")
.addListenerForSingleValueEvent(object: ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
dataSnapshot.children.forEach {
//"it" is the snapshot
val key: String = it.key.toString()
}
}
override fun onCancelled(p0: DatabaseError) {
//do whatever you need
}
})
That's because you're using a ValueEventListener
. If the query matches multiple children, it returns a list of all those children. Even if there's only a single matches child, it's still a list of one. And since you're calling getKey()
on that list, you get the key of the location where you ran the query.
To get the key of the matches children, loop over the children of the snapshot:
mDatabase.child("clubs")
.orderByChild("name")
.equalTo("efg")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String clubkey = childSnapshot.getKey();
But note that if you assume that the club name is unique, you might as well store the clubs under their name and access the correct one without a query:
mDatabase.child("clubs")
.child("efg")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String clubkey = dataSnapshot.getKey(); // will be efg
If this key (asdasdasddsad) also had the name:"efg" will club key will become asdasdasddsad and KLpcURDV68BcbAvlPFy
firebaseDatabase = FirebaseDatabase.getInstance();
mReference = firebaseDatabase.getReference("clubs");
mReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
String key = ds.getKey();
}
}
}
We can use HashMap.
initialize:
String for storing Key value. ModelClass for storing you data.
val hashMap: HashMap<String, "Your Model Class"> = 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)
Here is how I did it in Kotlin
var ref = database.child("clubs")
val database_listener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.children) {
val key = ds.key
}
}
}
ref.addValueEventListener(database_listener)