Это приложение для заказа еды, стоимость уже указана при вызове api. Я хочу рассчитать общую сумму рупий за все продукты в тележке. Это действие корзины:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cart)
txtChoose = findViewById(R.id.txtChoose)
btnPlace = findViewById(R.id.btnPlace)
recyclerCart = findViewById(R.id.recyclerCart) as RecyclerView
layoutManager = LinearLayoutManager(this)
dbCartList = RetrieveCart(this).execute().get()
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
supportActionBar?.title = "My Cart"
if (intent != null) {
restName = intent.getStringExtra("restName")
// intent.getStringExtra("addToCart")
}
txtChoose.text=restName
if (this !=null){
recyclerAdapter = CartRecyclerAdapter(this, dbCartList)
recyclerCart.adapter = recyclerAdapter
recyclerCart.layoutManager = layoutManager
}
// } catch (e: Exception) {
// Toast.makeText(
// this,
// "Some Error occurred!!",
// Toast.LENGTH_SHORT
// ).show()
// }
btnPlace.setOnClickListener {
DeleteCart(this, dbCartList as ArrayList<CartEntity>).execute().get()
}
}
class RetrieveCart(val context: Context): AsyncTask<Void, Void, List<CartEntity>>() {
override fun doInBackground(vararg p0: Void?): List<CartEntity> {
val db = Room.databaseBuilder(context, CartDatabase::class.java, "dish-db").build()
val result =db.cartDao().getAllDish()
return result
}
}
class DeleteCart(val context: Context, val cartEntity:ArrayList<CartEntity>): AsyncTask<Void, Void, List<CartEntity>>() {
override fun doInBackground(vararg p0: Void?): List<CartEntity>? {
val db = Room.databaseBuilder(context, CartDatabase::class.java, "dish-db").build()
for(i in 0 until cartEntity.size){
db.cartDao().deleteOrders(cartEntity[i].restId)
return null
}
return null
}
}
}
А это файл XML. Я хочу, чтобы сумма была заключена в скобки, написанные после кнопки «Разместить заказ». Что мне делать, чтобы он рассчитал общую сумму.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.CartActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
<TextView
android:id="@+id/txtChoose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Restaurant Name:"
android:layout_below="@id/toolbar"
android:textColor="#000"
android:textSize="25sp"
android:layout_marginLeft="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerCart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtChoose"
android:padding="10dp"
android:layout_margin="5dp"/>
<Button
android:id="@+id/btnPlace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorAccent"
android:text="Place order()"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"
/>
</RelativeLayout>