jeudi 9 mars 2023

Get a list of data from a dynamic checkbox in Kotlin

need help. I want to get data on toppings from a dynamic checkbox. When a user clicks the order button, i want the data checked in the checkbox to be sent to another activity. As in the following image.

From Detail Activity :

enter image description here

To Checkout Activity :

enter image description here

I've got the topping list using ryclerview on the Detail Activity, so the checkbox indicates that the data is dynamic.

This is my Adapter :

class AdapterTopping(val listTopping: List<DataItem?>?) : RecyclerView.Adapter<AdapterTopping.MyViewHolder>() {
    class MyViewHolder(view : View) : RecyclerView.ViewHolder(view){
        val checkbox = view.findViewById<CheckBox>(R.id.ch_box)
        val price_topping = view.findViewById<TextView>(R.id.tv_price_topping)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_topping,parent,false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        if (listTopping != null){
            return listTopping.size
        }
        return 0
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        
        holder.checkbox.text = listTopping?.get(position)?.attributes?.name

        val priceTopping = listTopping?.get(position)?.attributes?.price
        val localID = Locale("in", "ID")
        val numberFormat = NumberFormat.getCurrencyInstance(localID)
        numberFormat.minimumFractionDigits = 0
        holder.price_topping.text = numberFormat.format(priceTopping)

    }
}

This is my Detail Activity :

val btn_pesan = findViewById<AppCompatButton>(R.id.btn_pesan)
        btn_pesan.setOnClickListener {
            val intent = Intent(it.context,CheckoutActivity::class.java)
            startActivity(intent)
            finish()
        }
        
        ApiConfigFood.getService().getToppings(id).enqueue(object : Callback<ResponseFoods>{
            override fun onResponse(call: Call<ResponseFoods>, response: Response<ResponseFoods>) {
                if (response.isSuccessful){
                    if (response.body()?.data?.isNotEmpty() == true){
                        val responseBody = response.body()
                        val responseList = responseBody?.data
                        val adapterTopping = AdapterTopping(responseList)
                        rv_topping.apply {
                            layoutManager = LinearLayoutManager(this@DetailActivity)
                            setHasFixedSize(true)
                            adapterTopping.notifyDataSetChanged()
                            adapter = adapterTopping
                        }
                    }
                    else{
                        // not found data
                    }
                }
                else{
                    // not found data
                }
            }

            override fun onFailure(call: Call<ResponseFoods>, t: Throwable) {
                // error server
                Toast.makeText(this@DetailActivity, t.localizedMessage, Toast.LENGTH_SHORT).show()
            }

        })

List of toppings for Textview like the image below:

enter image description here




Aucun commentaire:

Enregistrer un commentaire