0

I have created adapter which is fetching data from firestore. But I need to paginate it in kotlin, can you help me?

enter image description here

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()
            }
        }
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • Refer this answer :- https://stackoverflow.com/a/44796538/3946958 – Ravindra Kushwaha Sep 20 '18 at 05:33
  • @RavindraKushwaha That answer discusses Realtime Database, not Firestore. – Doug Stevenson Sep 20 '18 at 05:35
  • @DougStevenson I have used the firebase in that link..Stored data in firebase and retrieving from the limit like 10 – Ravindra Kushwaha Sep 20 '18 at 05:36
  • There is a sample app I wrote that paginates Firestore and Realtime Database queries using the Android Jetpack paging library (among many other things). It's a lot of code, and there are limits to how you can do it, but it can be done. https://github.com/CodingDoug/firebase-jetpack/ – Doug Stevenson Sep 20 '18 at 05:37
  • @RavindraKushwaha I think you misunderstand what I'm saying. Firestore and Firebase Realtime Database are different database products. The question is asking about Firestore, but your provided link talks about Realtime Database. – Doug Stevenson Sep 20 '18 at 05:38
  • @DougStevenson ok thanks for info :) – Ravindra Kushwaha Sep 20 '18 at 05:39
  • If you are interested, **[this](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo Sep 20 '18 at 09:59

1 Answers1

1

This works for me. For Firestore pagination.

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)
            }
    }