Android|Kotlin recyclerView update one item

I have this issue:

I'm using a recycler view in my app but the thing is that I need to update one single item of my recycler view.

When I click on the recycler view item, it sends me to another screen where I update the value and returned (this is working, I get the values on back)

I tried to add a new function inside my adapter as follows:

public fun updateItem(updatedOI: ArrayList<OrderItem>){
        orderItem = updatedOI
        notifyDataSetChanged()
    }

and then call the function from my fragment/activity but it is not working.

I basically need to grab the index somehow and update that single index what it is doing now is removing all of the items and just add the updated one.

How do I fix this issue?

Upvotes: 1

Views: 2650

Answers (1)

cutiko
cutiko

Reputation: 10507

The problem is you can't update the data by making it reference another.

fun update(items: List<OrderItem>) {
    orderItem.clear()
    orderItem.addAll(items)
    notifyDataSetChanged()

}

I'm using a list here because it is easier than working with arrays, your inner data should be a mutable list.

If you only want to update 1 item. You can get the index and then pass it back

//in the fragment/activity
intent.putExtra(INDEX, youClickedIndex)

and then set the result back like you are allegedly doing

fun update(item: OrdertItem, index: Int) {
    orderItem.add(index, item)
    notifyItemChanged(index)

}

Upvotes: 3

Related Questions