I am trying to iterate through the database documents and have the fields update with the click of the next button and previous button. As of right now, it's only happening with one and then it stops. I don't know why it only does it once and stops. Any reason as to why? Here is a snapshot of what I got right now.
This image is hardcoded in there with the code that is below here.
[![private lateinit var locationTextView: TextView
private lateinit var descriptionTextView: TextView
private lateinit var previousArrow: Button
private lateinit var nextArrow: Button
private lateinit var usersImage: ImageView][2]][2]
val db = FirebaseFirestore.getInstance()
db.collection("Post").orderBy("timestamp")
val docRef = db.collection("Post").document("Puppy")
docRef.get()
.addOnSuccessListener { document ->
if (document != null) {
Log.d("Exists", "DocumentSnapshot data: ${document.data}")
// locationTextView.text = document.getString("locationTitle")
locationTextView.setText(document.getString("Name"))
descriptionTextView.setText(document.getString("description"))
val imageName = "Puppy"
val storageRef = FirebaseStorage.getInstance().reference.child("Images/$imageName")
val localFile = File.createTempFile("tempImage", "jpeg")
storageRef.getFile(localFile).addOnSuccessListener {
val bitmap = BitmapFactory.decodeFile(localFile.absolutePath)
usersImage.setImageBitmap(bitmap)
}
}
else {
Log.d("noExist", "No Such Document")
}
}
However, I want to be able to implement these two buttons, so I can get rid of the code above and have it just pull from the database and navigate through each Post with the click of the buttons. I added this onto my next button and it is able to iterate only once. Is there a way that I can iterate through the size of the documents, that way I can go through all of them and not just once?
nextArrow.setOnClickListener {
val first = db.collection("Post").orderBy("timestamp").limit(1)
first.get()
.addOnSuccessListener { documentSnapshots ->
val lastVisible = documentSnapshots.documents[documentSnapshots.size() - 1]
val next = db.collection("Post").orderBy("timestamp").startAfter(lastVisible).limit(2)
next.get().addOnSuccessListener { documents ->
for (document in documents) {
Log.d(TAG, "${document.id} => $${document.data}")
locationTextView.setText(document.getString("Name"))
descriptionTextView.setText(document.getString("description"))
}
}
val imageName = "Puppy"
val storageRef = FirebaseStorage.getInstance().reference.child("Images/$imageName")
val localFile = File.createTempFile("tempImage", "jpeg")
storageRef.getFile(localFile).addOnSuccessListener {
val bitmap = BitmapFactory.decodeFile(localFile.absolutePath)
usersImage.setImageBitmap(bitmap)
}
}
}
previousArrow.setOnClickListener {
}
Here is a snapshot of my database.
Again, to clarify, I am trying to iterate through my database with the Posts with the click of those buttons. Also, the images are stored in a firebase storage database since it's too large for firestore.