简体   繁体   中英

How to update editText for data object in recycle view Kotlin

I am new to android studio and just starter 1 month ago for learning it.

I currently try to written the app that has list of data class with name, price, quantity and unit cost. I have done the recycle view and i am facing the problem that how to update the editText value whenever the user input the quantity in the data classs

Below are my data class code

data class food (val food: String, val price: Int, var quantity: Int, var cost: Int = price*quantity)

object ABC {
var food_List = listOf<food>(
    ABC ("A", 28 ,5) ,
    ABC ("B", 28, 0)
)}   

Below are my recycle view adaptor

class customAdaptor (val context: Context, val food_List:List<food>):RecyclerView.Adapter<customAdaptor.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val v = LayoutInflater.from(context).inflate(R.layout.list_layout,parent, false)
    return ViewHolder(v)
}

override fun getItemCount(): Int {
    return food_List.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val listused=  food_List[position]
    holder.setData(listused,position)
}

inner class ViewHolder (itemView: View): RecyclerView.ViewHolder(itemView) {

    fun setData(listused: food?, pos: Int) {
        itemView.listFoodName.text = listused!!.food
        itemView.listUnitPrice.text =listused.price.toString()
        itemView.listUnitTotalPrice.text=listused.cost.toString()


    }
}}

Below are my mainactivity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_page_budget)

    val layoutManager = LinearLayoutManager(this)
    layoutManager.orientation = LinearLayoutManager.VERTICAL
    recyclerview.layoutManager = layoutManager


    val adapter = customAdaptor(this, ABC.food_List)
    recyclerview.adapter = adapter}

Below are my layout for card view.

<androidx.cardview.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="270dp"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:id="@+id/listFoodName"/>
        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:id="@+id/listUnitPrice"/>
         <EditText
                  android:layout_width="50dp"
                  android:layout_height="wrap_content"
                  android:textAlignment="center"
                  android:inputType="numberDecimal"
                  android:id="@+id/listQuantity"/>
        <TextView
                  android:layout_width="50dp"
                  android:layout_height="wrap_content"
                  android:textAlignment="center"
                  android:id="@+id/listUnitTotalPrice"/> -->

    </LinearLayout>
</androidx.cardview.widget.CardView>

Then i didnt know how to write the editText for the update. Please help. Thank you very much.

You should use this:

editText.afterTextChanged{doSomethingWithText(it)}

Your code would be something like:

fun setData(listused: food?, pos: Int) {
    itemView.listFoodName.text =listused!!.food
    itemView.listUnitPrice.text =listused.price.toString()         
    itemView.listUnitTotalPrice.text=listused.cost.toString()
    itemView.listQuantity.afterTextChanged { 
        listused?.quantity = it.toInt()
        itemView.listUnitTotalPrice.text=listused.cost.toString()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM