Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.4 Edit Shopping Items

In this lesson, you’ll learn how to add event handlers to the various widgets within RecyclerView layouts. Inside those event handlers, we’ll be writing code to edit data which is already present in the Realm database.

Related Links

3.4 Edit Shopping Items

Hello and welcome back. Right now nothing happens if you click on the icons or the check boxes in this list. In this lesson, you will learn how to add event handlers to the UI widgets which are inside our recycler view. Inside those event handlers, we will be writing cord which can edit the data which is already in their own database. Let's work on the check boxes first. So, go to the onBindView holder method and get inside the block for the active item view holder. To handle changes in the ticked state of item status, call the set onCheckedChangeListener method and passed a new instance of onCheckedChangeListener to it. This boolean parameter here represents the checked status. I will rename it to checked. So, if the checked status is true then we must change the completed field in shopping item to true. And because this is going to be a database write operation, we need to begin a new realm transaction now. Next, call set completed on the current item and pass through to it. Let us also update the timestamp using the set timestamp and system current time Milly's methods. Now, that we have made the required changes we can commit the transaction to make those changes permanent. Additionally we need to re initialize the dataset in order to load those changes. And then call Adapter.notifyDataSetChanged to update the contents of the RecyclerView. Let us now work on the undo image view. So, inside the block for the InactiveItemViewHolder, call setOnClickListener to add a click handler to the itemAction image view. The code we need to write here is going to be almost identical to the code we wrote for the checkbox. So, we just copy all of it and paste it here. The only change we need to make is instead of passing true to set completed, we must pass false. This of course makes the inactive item, active again. And now let us work on the edit image view. So, inside the block for the active item view holder call setOnClickListener to add a new click handler to the item action image view. When the user starts this icon they should be taken to the item activity. Therefore, we need to launch item activity from here. To do so create a new intent using main activity as the context and item activity as the class. This time the title of item activity should be edit item, so call put extra and add an extra call title, whose value is edit item. When the item activity opens this way, its item name and item quantity fields must be initialized to the item name and quantity of the current item. Therefore we must pass both the item name and the item quantity as extras to this intent. I will simply use ITEM_NAME and ITEM_QUANTITY as the names of these extras. Additionally, let us pass the ID of the current item as an extra card item ID. You can use getId to get the ID of the current item and then launch item activity using the start activity for result method. You can again pass any number you want as the request code. We should now write some code to handle these extras inside ItemActivity. So, open ItemActivity.java, and inside the onCreate method, check if the intent has an extra called ITEM_NAME. If it is present, call setText of inputItemName to initialize it to the value of item name. You can get the value using the getStringExtra method. Next repeat the same steps to initialize input item quantity to the value of item quantity. When the user presses the save button now we need to know whether we have to add a new item or edit an existing one. Therefore, let us create a boolean member variable called edit mode, by default it can be false. If the intern contains an extra called item name. It means we are editing, so set edit mode to true here. Let me quickly make the boolean try it. Okay, now in the on options item selected method, we can add the edit mode boolean to this condition. So if edit mode is false, it means we are adding a new item, and nothing needs to change here. Copy all of this code and paste it. In this condition here, we will check if edit mode is true. If it is, then a few changes need to be made. First, instead of calling realm.createObject to create a new object. We need to call realm.where to find an already existing object. Then call equal to, to find the object whose ID matches the value present in the intents item ID extra. Finally, call findfirst to fetch the first item which matches this ID. And remove these two lines of code here because we shouldn't be reassigning the ID of the completed fields. And that's all you need to do. Main activity .Java already has the code to update the contents of the recycle of you after the chance. You can run the app now and click on any checkbox. The item instantly will move to the recently completed section. And if you click on the Undo icon, It goes back to the active item section. You can also click on the Edit icon to edit any item. Any changes you make will be instantly visible in the home screen. You now know how to add event handlers to widgets inside a recycler view.. In this lesson, you also learned how to update data in [INAUDIBLE] database. Thanks for watching.

Back to the top