Display target data from firebase

2019-08-27 02:14发布

问题:

I display a list of my targets in fragmentA, when I click on one of them, I pass the guid of this target to the fragmentB

After that, I try in the fragmentB display the data of this target for this guid:

private fun fetchTarget(guid: String) {
        val uid = firebaseUser!!.uid
        // Attach a listener to read the data at the target id
        databaseReference?.child("targets")?.child("users")
            ?.child(uid)?.child("targets")?.child(guid)?.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                val data = dataSnapshot.value as? HashMap<String, String>?
                val name = data?.get("name") ?: ""
                val description = data?.get("description") ?: ""

                if (name.isEmpty()) Log.d("some", "nameIsEmpty")
                else updateViewsContent(name = name, description = description)
            }

            override fun onCancelled(databaseError: DatabaseError) {
                Log.d("some", databaseError.message)
            }
        })
    }

Here I get the guid: -LmfEVnwgqCUqt7beHDg And in my console i have next structure:

Unfortunately I can't display data of target, though like all the chains I installed

Q: How i can download -LmfEVnx-y7c3oh8_U9F ?

回答1:

To display the data that belongs to a single guid, you should use a query and then iterate through the DataSnapshot object like in the following lines of code:

val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseDatabase.getInstance().reference
val targetsRef = rootRef!!.child("targets").child("users").child(uid).child("targets")
val query = targetsRef.orderByChild("guid").equalTo("-LmfEVnwgqCUqt7beHDg")
val valueEventListener = object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        for (ds in dataSnapshot.children) {
            val target = ds.getValue(Target::class.java)
            Log.d(TAG, target.name)
        }
    }

    override fun onCancelled(databaseError: DatabaseError) {
        Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
    }
}
query.addListenerForSingleValueEvent(valueEventListener)

The result in the logcat will be:

fgg


回答2:

You are not getting any data because to get above data your guid value in query should be "-LmfEVnx-y7c3oh8_U9F" but you are passing "-LmfEVnwgqCUqt7beHDg".

You can try below query to get above data:

val uid = firebaseUser!!.uid
        // Attach a listener to read the data at the target id
        databaseReference?.child("targets")?.child("users")?.child(uid)?.child("targets")?.orderByChild("guid").equalTo(guid)?.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                val data = dataSnapshot.value as? HashMap<String, String>?
                val name = data?.get("name") ?: ""
                val description = data?.get("description") ?: ""

                if (name.isEmpty()) Log.d("some", "nameIsEmpty")
                else updateViewsContent(name = name, description = description)
            }

            override fun onCancelled(databaseError: DatabaseError) {
                Log.d("some", databaseError.message)
            }
        })