i am trying to code an admin section for a food ordering app where they could see the orders that are stored in a firestore database. To do that i have some code to listen the database and get all documents:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Window
import android.widget.Toast
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
class AdminOrders : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
supportActionBar?.hide()
setContentView(R.layout.activity_admin_orders)
val db = Firebase.firestore
db.collection("Orders").addSnapshotListener { snapshot, e ->
if (e == null && snapshot != null) {
db.collection("Orders")
.get()
.addOnSuccessListener { result ->
for (document in result) {
Toast.makeText(this@AdminOrders, document.data["food"].toString(), Toast.LENGTH_SHORT).show()
}
}
}
}
}
}
the problem is that to make the ui i would need an xml button that when clicked show the fields of the document for each returned document in the database.
How could i acheive this result in android studio with kotlin ?
2
Answers
You can create a RecyclerView to show orders in a list. Here’s a sample app source in Kotlin.
The item layout will have a button. Then set the Click Listener to show/hide other elements.
Your adapter might look like this:
30 minutes of training today could save you months of coding this year.
If you’re in the beginning stages of your app, this is the most important thing you’ll read all day.
You need to look into the Android Composable library. Most of the Kotlin community has ditched XML in exchange for Composable’s function-based UIs. It makes it way easier to connect your data, let alone build buttons. You’ll never use a
RecyclerView.Adapter
again (hopefully).PS: I waited 26 minutes to post this. That’s how good it is.