【问题标题】:how can i paginate data in kotlin using firestore?如何使用 Firestore 在 kotlin 中对数据进行分页?
【发布时间】:2018-09-20 05:31:59
【问题描述】:

我创建了从 Firestore 获取数据的适配器。 但是我需要在kotlin中分页,你能帮我吗?

private fun fetch(){
            try {
                mShared = getSharedPreferences("mShared", 0)
                val path = mShared!!.getString("governorate", "Suez").toString()
                dp!!.collection("Fraise")
                        .whereEqualTo("governorate", "${path}")
                        .orderBy("time")
                        .limit(5)
                        .get()
                        .addOnCompleteListener {
                            data.addAll(it.result.toObjects(Data::class.java))
                            adapter = Fraise_adapter(this, data)
                            adapter.notifyDataSetChanged()
                            recyclerView.adapter = adapter
                        }
            } catch (e: Exception) {
                Toast.makeText(this, "Please choose a governorate from the main activity", Toast.LENGTH_LONG).show()
            }
        }

【问题讨论】:

  • 参考这个答案:- stackoverflow.com/a/44796538/3946958
  • @RavindraKushwaha 该答案讨论的是实时数据库,而不是 Firestore。
  • @DougStevenson 我已经在该链接中使用了 firebase。将数据存储在 firebase 中并从 10 之类的限制中检索
  • 我编写了一个示例应用程序,它使用 Android Jetpack 分页库(以及许多其他东西)对 Firestore 和实时数据库查询进行分页。它有很多代码,而且你如何做到这一点是有限制的,但它是可以做到的。 github.com/CodingDoug/firebase-jetpack
  • @RavindraKushwaha 我想你误解了我的意思。 Firestore 和 Firebase 实时数据库是不同的数据库产品。问题是关于 Firestore,但您提供的链接是关于实时数据库的。

标签: android firebase kotlin google-cloud-firestore


【解决方案1】:

这对我有用。用于 Firestore 分页。

private fun first(){

          val first = collectionRef
                .orderBy("priority")
                .limit(3)

        first.get()
            .addOnSuccessListener {

                    var  lastVisible = it.documents[it.size()-1]
                var text = ""
                for (document in it) {
                    val note = document.toObject(Note::class.java)
                    note.noteId = document.id
                    text+= note.title+"\n"

                }
                binding.tvShow.append(text)

                binding.btnShow.setOnClickListener {

                    val  next = collectionRef
                        .orderBy("priority")
                        .startAfter(lastVisible)
                        .limit(3)

                    next.get()
                        .addOnSuccessListener {
                            var text = ""
                            for (document in it) {
                                val note = document.toObject(Note::class.java)
                                note.noteId = document.id
                                text+= note.title+"\n"
                            }

                            if(it.size()>0) {
                                text += "--------------------------\n\n"
                                binding.tvShow.append(text)
                                lastVisible = it.documents[it.size()-1]
                            }
                        }
                }
            }
            .addOnFailureListener {
                Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show()
                Log.d(TAG, it.message)
            }
    }

【讨论】:

    猜你喜欢
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2020-10-09
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多