RecyclerView – Modify item

In this chapter of the RecyclerView andorid tutorial we are going to implement how we can modify an item of the list. When we the tap an item, then the same dialog will popup, what we can use to create a new item.

In this case, the dialog will be filled out with the data of the clicked item, and the action button of Modify will change the properties of the item and notify the RecyclerView about the change.


If you haven’t done the previous chapters, then from GitHub you can download the starter project for this chapter.

GitHub – swipe_and_reorder_items branch

Step 1 – Get the arguments

First in this chapter, we gonna save the arguments of toDo and position. In the chapter of Adapter+ViewHolder we have already added these arguments to the action. Just to remind you, inside of the ViewHolder, we have implemented the click listener also. So, if you click on an item inside of the RecyclerView, then the selected ToDo item and its position gonna passed to the dialog. You can find this part of the code inside of the ToDoAdapter::class.

Next, open the AddToDoDialog::class and paste the next few lines into the onViewCreated() method.

get the arguments

As a fragment navigator in this tutorial, we are using the Navigation Component library. This library allows us to get back the arguments of the fragment. Previously, we have added the safe args depenedency also to the project. Using this dependency, we can get safely the arguments. Then, the next 2 rows will create an instance about the passed data. 

Step 2 – Populate the form

Next step is to populate the form with the data. Because the toDo argument can be null as well, we have to make a safe call using the let lambda. It means, the code inside of the let lambda won’t be executed, if the value is null. Because of this, we can avoid the NullPointerException.

Copy and paste the below code under the previously declared variables and after the initSpinner() method.

The safe call
After that, you will have the populateForm() in red. It means, we haven’t implemented it yet. So move your cursor to the call of the function and at the left side click on the red bulb.

Then select the second option, which is “Create function ‘populateForm'”. Android Studio will create automatically with arguments for the method. Finally, raname the argument of the ToDo to toDo.

Next is the body of populateForm() method. Copy and paste there the below code.


Step 3 – Press the button

In this step, we will modify the click listener of the btn_add button. Currently, when we push this button, a new item will be created, but we want to modify the opened one. So, we have to tell for the app, what we want to do. Remember? The toDo item is always null when we create a new item, and it isn’t null, when we have passed the argument, so if we modify an item.

With this in mind, we just need to check the value of the toDo variable. So, refactor the click listener of the btn_add button using the below code .


Here, we will have again the updateToDo() method in red.

Step 4 – Update item using the ViewModel

As we have done before, all of the operations on the list of the RecyclerView happen inside of the ToDoViewModel class.

So, move again the cursor to the updateToDo() method, then click on the red bulb on the left side of the line, and select “Create member function ‘ToDoViewModel.updateToDo’“.

Now, modify the argument of “createToDo” to “toDo”. Then paste the below code into the body of this method.


The first line will set the item in the corresponding position. Then the second value will change the value of the MutableLiveData, which will notify the RecyclerView inside of RecyclerFragment about the change.

Run the app

Finally run the app. When you select an item from the list, then in the dialog you should see the selected item. Then, if you make some changes, then the selected item will be modified in the list.


The source code is available on GitHub

GitHub – modify_item branch


I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂



Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *