如何从Firebase android kotlin获取所有具有特定值的孩子? [英] how to get all child with specific value from Firebase android kotlin?

查看:30
本文介绍了如何从Firebase android kotlin获取所有具有特定值的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从数据库中的所有用户获得图像中的颜色项.我尝试

I want to get just color item in the image from all user in my database. I try this

private fun setListPersonOfferService(){
    val database = FirebaseDatabase.getInstance()
    val userIdReference = database.getReference("user personal data").child("Country")
    val userFilterListener = object : ValueEventListener {
        override fun onCancelled(databaseError: DatabaseError) {
            // handle error
        }
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            val listTest = dataSnapshot.children.map { it.getValue(HomePageModel::class.java)!! }

                itemAdapter = ItemAdapterHomePage(context ?: return,
                    listTest as ArrayList<HomePageModel>
                )
                itemAdapter?.onItemClick = {
                    Supplier.serviceList[it].state = !Supplier.serviceList[it].state

                    itemAdapter?.notifyDataSetChanged()
                    Toast.makeText(context, "go to speck", Toast.LENGTH_LONG).show()
                }

                recyclerView_homePage.apply {
                    layoutManager = LinearLayoutManager(context)
                    adapter = itemAdapter
                }
            }
        }
    userIdReference.addListenerForSingleValueEvent(userFilterListener)
}

class HomePageModel(var lastName: String?=null,
                     var town: String?=null,
                     var faculty: String?=null,
                     var languageOfStudy: String?=null )

如果我更改了对特定目录的引用 .child("Turcia").child("Adana").child(ÇukurovaUniversity").child(商学院")可行,但我想从所有用户那里获取.

if i change Reference to the specific directory .child("Turcia").child("Adana").child("Çukurova University").child("Faculty of Business") it works, but i want to get from all user.

推荐答案

您需要在 onDataChange 中的 dataSnapshot 上导航,以获取正确的子节点

You'll need to navigate over the dataSnapshot in your onDataChange to get to the correct child nodes.

要导航 DataSnapshot 对象,您有两种主要方法:

To navigate a DataSnapshot object you have two main approaches:

  1. 您已经知道要访问的孩子的密钥,在这种情况下,您可以使用在JSON中读取已标记的子级,类似于您已经尝试过的子级参考.
  2. 您不知道子节点的键,在这种情况下,您需要遍历收听以下内容的Firebase文档:重视儿童名单上的事件.
  1. You already know the key of the child you want to access, in which case you use the child() method to access that specific child node. For example, you could read the marked child in the JSON with dataSnapshot.child("Turcia/Adana/Çukurova University/Faculty of Business").children.map, similarly to what you already tried on the reference.
  2. You don't know the key of the child nodes, in which case you loop over the getChildren() property of the snapshot. For an example of the latter, see the Firebase documentation on listening for value events on a list of children.

我认为您正在这里寻找第二种方法,但是无论如何,您在处理 DataSnapshot s时,都有一些方法可以获取其中的数据.

I think you're looking for the second approach here, but in any case you're dealing with DataSnapshots, there are the ways to get at the data in there.

这篇关于如何从Firebase android kotlin获取所有具有特定值的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆