从Firebase实时数据库子级获取数据

时间:2019-06-20 00:49:12

标签: firebase firebase-realtime-database kotlin

这是我的项目,我有2个RecyclewView,第一个选择了我要操作的用户,它运行良好。 选择它时,我将存储您的ID,并将其转移到下一个屏幕,该屏幕应显示您的病史(使用您的ID在数据库中搜索您的病史)。 但是,这个回收站视图仍然是空白的,没有给出错误但也没有返回任何内容,也许我错过了放置该历史记录的时间。

image BD Firestore:[1]:https://imgur.com/HOxQTsI

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_verificar_historico)

    setSupportActionBar(toolbar_historico)
    toolbar_historico.setTitle("Titulo Aqui")

    userId = intent.extras?.getString("userId")


    textViewX.setText(userId)



    getLastKey()

    val layoutManager = LinearLayoutManager(this)
    recycler_view_historico.layoutManager = layoutManager

    val dividerItemDecoration = DividerItemDecoration(recycler_view_historico.context, layoutManager.orientation)
    recycler_view_historico.addItemDecoration(dividerItemDecoration)

    adapter = HistoricoAdapter(this)
    recycler_view_historico.adapter = adapter

    getHistorico()

    recycler_view_historico.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            total_item = layoutManager.itemCount
            last_visible_item = layoutManager.findLastVisibleItemPosition()

            if (isLoading && total_item <= last_visible_item + ITEM_COUNT) {
                getHistorico()
                isLoading = true
            }
        }
    })
}


private fun getHistorico() {
    if (!isMaxData) {
        val query: Query
        if (TextUtils.isEmpty(last_node))
            query = FirebaseDatabase.getInstance().reference
                .child("pacientes/$userId/historico")
                .orderByKey()
                .limitToFirst(ITEM_COUNT)
        else
            query = FirebaseDatabase.getInstance().reference
                .child("pacientes/$userId/historico")
                .orderByKey()
                .startAt(last_node)
                .limitToFirst(ITEM_COUNT)

        query.addListenerForSingleValueEvent(object : ValueEventListener {
            override fun onCancelled(p0: DatabaseError) {
            }

            override fun onDataChange(p0: DataSnapshot) {
                if (p0.hasChildren()) {
                    val historicoLista = ArrayList<HistoricoRecycle>()
                    for (snapshot in p0.children)
                        historicoLista.add(snapshot.getValue(HistoricoRecycle::class.java)!!)
                    last_node = historicoLista[historicoLista.size - 1].data

                    if (!last_node.equals(last_key))
                        historicoLista.removeAt(historicoLista.size - 1)
                    else
                        last_node = "end"

                    adapter.addAll(historicoLista)
                    isLoading = false
                } else {
                    isLoading = false
                    isMaxData = true
                }
            }

        })
    }
}

private fun getLastKey() {

    val get_last_key = FirebaseDatabase.getInstance().getReference()
        .child("pacientes/$userId/historico")
        .orderByKey()
        .limitToLast(1)
    get_last_key.addListenerForSingleValueEvent(object : ValueEventListener {
        override fun onCancelled(p0: DatabaseError) {
        }

        override fun onDataChange(p0: DataSnapshot) {
            for (userSnapShot in p0.children)
                last_key = userSnapShot.key
        }
    })
}

0 个答案:

没有答案