skip to Main Content

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


  1. 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:

    
    class OrdersAdapter(private val dataSet: Array<Orders>) :
            RecyclerView.Adapter<OrdersAdapter.ViewHolder>() {
    
        class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val textView: TextView
    
            init {
                // Define click listener for the ViewHolder's View
                textView = view.findViewById(R.id.textView)
                button = view.findViewById(R.id.button)
            }
        }
    
        // Create new views (invoked by the layout manager)
        override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
            // Create a new view, which defines the UI of the list item
            val view = LayoutInflater.from(viewGroup.context)
                    .inflate(R.layout.order_item, viewGroup, false)
    
            return ViewHolder(view)
        }
    
        // Replace the contents of a view (invoked by the layout manager)
        override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
    
            // Get element from your dataset at this position and replace the
            // contents of the view with that element
            viewHolder.textView.text = dataSet[position].title
            // Set the button handler OnClickListener
            viewHolder.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {
                      // Show other elements
                  }
              });
    
        }
    
        // Return the size of your dataset (invoked by the layout manager)
        override fun getItemCount() = dataSet.size
    }
    
    
    
    Login or Signup to reply.
  2. 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).

    @Composable
    fun MyFirstButton(){
        Button(
        onClick = { /* ... */ },
    ) {
        // Inner content including an icon and a text label
        Icon(
            Icons.Filled.Favorite,
            contentDescription = "Favorite",
            modifier = Modifier.size(ButtonDefaults.IconSize)
        )
        Spacer(Modifier.size(ButtonDefaults.IconSpacing))
        Text("Like")
    }
    

    PS: I waited 26 minutes to post this. That’s how good it is.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search