Advertisement
Guest User

Untitled

a guest
Oct 26th, 2021
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.33 KB | None | 0 0
  1. package com.kodeplay.mytrial
  2. import android.os.Bundle
  3. import android.widget.Button
  4. import androidx.activity.ComponentActivity
  5. import androidx.activity.compose.setContent
  6. import androidx.compose.foundation.layout.Column
  7. import androidx.compose.material.Button
  8. import androidx.compose.material.MaterialTheme
  9. import androidx.compose.material.Surface
  10. import androidx.compose.material.Text
  11. import androidx.compose.runtime.*
  12. import androidx.compose.runtime.setValue
  13. import androidx.compose.runtime.snapshots.SnapshotStateList
  14. import androidx.compose.ui.tooling.preview.Preview
  15. import com.kodeplay.mytrial.ui.theme.MytrialTheme
  16. data class MenuItem (
  17.     val itemName:String ="",
  18.     val itemDesc:String="",
  19.     val itemPrice:Double=0.0,
  20.     val itemPhoto:String="",
  21.     var itemCartQuantity:Int=0,
  22.     var itemIndex:Int=0
  23. )
  24. class MainActivity : ComponentActivity() {
  25.     override fun onCreate(savedInstanceState: Bundle?) {
  26.         super.onCreate(savedInstanceState)
  27.  
  28.         setContent {
  29.             var menuItemList = remember { mutableStateListOf <MenuItem>() }
  30.             menuItemList.add(
  31.                 MenuItem (
  32.                     itemName = "samosa",
  33.                     itemDesc = "samosa",
  34.                     itemPrice = 10.0,
  35.                     itemPhoto = "",
  36.                     itemCartQuantity = 0,
  37.                     itemIndex=0
  38.                 )
  39.             )
  40.             menuItemList.add(
  41.                 MenuItem (
  42.                     itemName = "vada",
  43.                     itemDesc = "samosa",
  44.                     itemPrice = 10.0,
  45.                     itemPhoto = "",
  46.                     itemCartQuantity = 0,
  47.                     itemIndex=1
  48.                 )
  49.             )
  50.             fun updateCount (index:Int)
  51.             {
  52.                 println ("update count called with $index")
  53.                menuItemList[index].itemCartQuantity+=1
  54.                 println(menuItemList[index])
  55.             }
  56.        
  57.             Column {
  58.                 for (myItem in menuItemList) {
  59.                     Column {
  60.                         Text("${myItem.itemName}")
  61.                         Text("${myItem.itemCartQuantity}")
  62.                         Button(onClick = { updateCount(myItem.itemIndex) }) {
  63.                         }
  64.                     }
  65.  
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement