How to get the key from the value in firebase

前端 未结 7 2000
挽巷
挽巷 2020-12-01 10:04

How do I get the key \"-KLpcURDV68BcbAvlPFy\" when I know the field \"name\" contains \"efg\" in the following structure in Firebase.

clubs
    -KLpcURDV68Bc         


        
相关标签:
7条回答
  • 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
                }
             })
    
    0 讨论(0)
  • 2020-12-01 10:17

    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
    
    0 讨论(0)
  • 2020-12-01 10:17

    If this key (asdasdasddsad) also had the name:"efg" will club key will become asdasdasddsad and KLpcURDV68BcbAvlPFy

    0 讨论(0)
  • 2020-12-01 10:19
    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 10:20

    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)
    
    0 讨论(0)
  • 2020-12-01 10:27

    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)
    
    0 讨论(0)
提交回复
热议问题