Ad

How To Access Arraylist Data In Kotlin

- 1 answer

i have:

var itemOrder = ArrayList<String>()

file model Order.kt:

private var itemName:String? = null
private var note:String? = null
private var price: CharSequence? = null
private var all: Int? = null
private var quantity: Int? = null

fun Order(itemName: String, price: CharSequence, all: Int, note: String, quantity: Int)
    {
        this.itemName = itemName
        this.price = price
        this.all = all
        this.note = note
        this.quantity = quantity
    }

then i add for itemOrder like this:

itemOrder.add(Order.Order(finalHolder1.nameItem!!.text.toString(),`finalHolder1 priceItem!!.text, finalHolder1.quantity, finalHolder1.note!!.text.toString(), finalHolder1.quantity * hargaSatuan).toString())

then

fun cart(): ArrayList<String> {
    return itemOrder
}

i want to see all item value where i have add before to itemOrder in Toast like

itemName : valueofValue

price: valueofprice

all: valueofall

note: valueofnote

quantity: valueofquantity

Toast.makeText(applicationContext, "List Item : "+ cart(), Toast.LENGTH_LONG).show()

but i get this result: Result

i want change result of kotlin.init to value each array in array

Ad

Answer

Instead of primitive String type , arraylist should be "Order" of type;

 var itemOrder = ArrayList<Order>()

Instead of how you defined the model class, define as

data class Order(

      val itemName:String,
      val note:String,
      val price: CharSequence,
      val all: Int,
      val quantity: Int
);

// add item as

itemOrder.add(Order(finalHolder1.nameItem!!.text.toString(),`finalHolder1 priceItem!!.text, finalHolder1.quantity, finalHolder1.note!!.text.toString(), finalHolder1.quantity * hargaSatuan).toString())

// print each value as

fun( 0 in until itemOrder.size){

   Log.e("name",itemOrder[i].itemName);
   //get all data in the same way
}
Ad
source: stackoverflow.com
Ad