How can I make an item value update in real time? I have a list. Elements are added to it. (list of connected devices via bluetooth). Every cell contains information: name, date. How can I change, for example, the name in any cell without updating the all list? Now when I call update method information changes in the first list element (in position 0) but I need that I could change value any position and any element when I call update method. I read about DiffUtil class. But I'm not sure that is suitable option for me.
So, I have model class:
data class Device (
var name: String,
var date: String
)
adapter:
class CustomRecyclerAdapter(private val values: MutableList<Device>) :
RecyclerView.Adapter<CustomRecyclerAdapter.MyViewHolder>() {
override fun getItemCount(): Int {
return values.size
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.list_item, viewGroup, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(viewHolder: MyViewHolder, position: Int) {
var device: Device = values[position]
viewHolder.bin(device)
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var textName: TextView? = null
var textDate: TextView? = null
init {
textName = view.findViewById(R.id.name)
textDate = view.findViewById(R.id.date)
}
fun bin(device:Device) {
textName?.text = device.name
textDate?.text = device.date
}
}
class MyFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_upload_card, container, false)
var recyclerView = view.findViewById(R.id.recyclerView) as RecyclerView
recyclerView.layoutManager = LinearLayoutManager(activity)
adapter = CustomRecyclerAdapter(list)
recyclerView.adapter = adapter
return view
}
private fun addItem() {
var device = Device("DeviceName", "date")
list.add(0, device)
adapter?.notifyItemInserted(0)
}
fun update() {
list[0].run {
name = "newName"
date = "newDate"
}
adapter?.notifyDataSetChanged()
}
Maybe I have to use RX here? But I don't understand how in this case