如何解决我为大学项目创建了一个类似在线购物的应用程序我面临在购物车中实时更新的问题
enter image description here[这是我面临的问题,当我更新产品数量时,价格更新在顶部而不是底部,这是我的代码]
CartItemsAdapter.kt 文件
class cartItemsAdapter(context: Context,val cartItems: ArrayList<cartDB_offline>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
val context = context
class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
fun bind(item: cartDB_offline) {
val eachItemTotalPrice = item.product_price * item.product_quantity
itemView.cart_item_name.text = item.product_name
itemView.cart_item_price.text = eachItemTotalPrice.toString()
itemView.cart_item_count.text = item.product_quantity.toString()
// itemView.recyclerview_in_cart.setOnClickListener {
// val fp =
// }
}
}
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.cart_items,parent,false)
return ViewHolder(view)
}
产品的增减
覆盖 fun onBindViewHolder(holder: RecyclerView.ViewHolder,position: Int) { (持有人作为ViewHolder).bind(cartItems[position])
holder.itemView.decrement_in_cart.setOnClickListener {
val count = holder.itemView.cart_item_count.text.toString().toInt()
if(count >= 1) {
val databaseHelper = DatabaseHelper(it.context)
val item_name = holder.itemView.cart_item_name.text.toString()
val count_new = databaseHelper.increase_decrease_in_cart("decrement",item_name,count)
holder.itemView.cart_item_count.text = count_new.toString()
cartItems[position].product_quantity--
holder.bind(cartItems[position])
//PENDING -- Have to refresh price
}
}
holder.itemView.increment_in_cart.setOnClickListener {
val count = holder.itemView.cart_item_count.text.toString().toInt()
if(count < 10) {
val databaseHelper = DatabaseHelper(it.context)
val item_name = holder.itemView.cart_item_name.text.toString()
val count_new = databaseHelper.increase_decrease_in_cart("increment",count)
holder.itemView.cart_item_count.text = count_new.toString()
cartItems[position].product_quantity++
holder.bind(cartItems[position])
//PENDING -- Have to refresh price
}
}
Activity_cart.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:background="@color/white"
android:layout_height="match_parent"
tools:context=".Cart_Firebase">
<include layout="@layout/toolbar"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="65dp"
android:layout_marginBottom="60dp"
android:id="@+id/recyclerview_in_cart">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:weightSum="2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="4dp"
android:textSize="20sp"
android:padding="10dp"
android:textAlignment="center"
android:textColor="@color/black"
android:background="@drawable/input_styles"
android:id="@+id/total_price_tag"
android:gravity="center_horizontal" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="4dp"
android:onClick="BuyButtonClicked"
android:id="@+id/buy_button"
android:textAllCaps="false"
android:background="@drawable/cart_btn_style"
android:textSize="20sp"
android:text="Buy Now"/>
</LinearLayout>
</RelativeLayout>
its the error the price is updated in top but not in bottom
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。