How update value recyclerview element in real time?

Viewed 875

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

3 Answers

If I understood the question correctly, you need to call notifyItemChanged, instead of adapter?.notifyDataSetChanged(), this method has two different calls one with position and the other with position and payload for a partial change of the targeted item, please check it here

Suggested solution for your scenario.

I will discuss a few points here.

  • RecyclerView has many methods which can used for example add, remove, update, range update etc..

Your case you want to update item/items:

  • Single item/row: If you want to update a single row, you should have position for that row. This is your task, how you manage to get the
    position. For example, once you have got updated row data, you can
    search from the list and get position from there. Following methods can be used notifyItemChanged, notifyItemRangeChanged

  • All List: If you have got the whole list you can use the DiffUtil to update this list.

For realtime data you can use Livedata, RxJava but the above procedure will remain same. Using LiveData you can observe changing in the Database.

Let me know if you got your solution.

Please wrap your Data Source inside Live Data. And then pass this live data into Recycler View Adapter.

for e.g.
@Query("SELECT * FROM word_table ORDER BY word ASC")
fun getAlphabetizedWords(): LiveData<List<Word>>

Then Recycler View will take care of updating the data on screen when there are changes made to the Database.

Related