Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.3 Display Shopping Items

In this lesson, you’ll learn how to create a dataset for the RecyclerView. Then you’ll go on to display the shopping items saved in the Realm database.

Related Links

3.3 Display Shopping Items

Hello and welcome back to this course about building material design Android apps. In this lesson, you're going to learn how to display the shopping items which have been added to their own database. I'm going to show you how to query their own database, and then load the results of the query into a dataset which can be used by the recycle views adapter. To keep things simple, we will be using an array list as the dataset. Let's start by adding a list of shopping item objects to main activity. Call it dataset, and then create a new method to initialize the dataset. The first thing we need to do here, is assigned a dataset to a new instance of array list. At this point, we have an empty dataset, we should now fetch the shopping items from their own database, and add them to this dataset. However, we must fetch the active items and the completed items separately because the RecyclerView should display them in separate sections. Let's fetch the active items first. Create realm results variable which can hold shopping item objects and call it ActiveItemResults. The realm database has a very easy to use API to query it, to start a query, call realm.where and specify the class of the object you are looking for. For now, it's ShoppingItem.class, next use the equalTo method to look only for those shopping items whose completed field is set to false. Finally, fetch the results in a sorted manner using the findAllSorted method. To show the latest shopping items first sort by timestamp in the descending order. We can now fetch the inactive items in the same way. So, copy and paste all this code and simply change the second parameter of the equalTo method from false to true. We now have both the active and inactive shopping items. However, we're also going to need a dummy shopping item object to represent the subheader. So, create and initialize it using the constructor. Set it's timestamp field two minus one, this way we can easily identify it later. Okay, let's put all these results in the dataset in the correct order, first, loop through all the items in the ActiveItemsResults object, and then add them to the dataset using the add method. Next, add the subheader dummy object to the list. Finally, look through all the items in the InactiveResults object and add them to the dataset using the add method. Our dataset is ready. We must now configure the adapter so that it can use this dataset, inside the adapter implementation, go to the getItemCount method, this method must return the count of the items in the dataset. In other words, it must return the size of the dataset, so change its return value to dataSet.size. Our adapter has to work with three different types of layouts. But it still doesn't know which layout should be used for any given position. To solve this problem, override the getItemView type method. Inside the method, get the ShoppingItem at the given position using the dataSet.get method. Let's first check if this item is the subheader by checking if it's timestamp is equal to minus one. If true, we can return subheader view to let the adapter know that the subheader layout should be used for it. Next, let's check if the item has been completed using the isCompleted method. If this is true return INACTIVE_VIEW. If neither of those conditions is true, it means we can simply return ACTIVE_VIEW. Now, that the adapter knows when a particular layout should be used, we can start working on the contents of those layouts inside the onByViewHolder method. Let's again check if the current item represent the subheader. So, copy this code and paste it here. If it is the subheader. We don't need to do anything, so just return, next, if the current item is completed, it means we must set the contents of the inactive item layout. It also means that our ViewHolder is an inactive item ViewHolder, so typecast the holder parameter to InactiveItemViewHolder, next, set the item names content using the set text method. Use currentItem.getName to fetch the name of the current item. This item name needs a strike thru effect. To add the effectn call setPaintFlags. As an argument to it, pass the result of getPaintFlags to which you are the value of paint.strike_thru_text_flag, the inactive item is ready. So, let's move on to the active item now. You can simply copy the first two lines here to set the item name of the active item. Don't forget to change the type of the view holder to activeItemView holder. The active item must display the item quantity as well, so call itemQuantity.setText and pass currentItem.getQuantity to it. Aadditionally, we must make sure that the itemStatus checkbox is not checked by passing false to the said checked method. Our adapter is now ready to display the items in the dataset. So, go to the end of the onCreate method of the activity and call the initializeDataSet method, and then assign the shopping items adapter to the RecyclerView using the setAdapter method. If we run that, now we will be able to see the shopping items we added in the previous lesson. However, if we add another item now, say potatoes and 5 Kg and hit save, we won't be able to see it immediately. The item will show up only if you quit the app and open it again. This is because item activity does not tell main activity that it has added something to the database. To solve this problem, open item activity and just before the call to the finish method, add a call to the set result method. And pass RESULT_OK to it. Back in MainActivity, inside the FloatingActionButton's click handler, change the call to startActivityForResult. This method needs a request code, so you can pass any random integer here. I will say one, to handle the result of the item activity, override the onActivityResult method. Here, check if the result code is equal to RESULT_OK. If true, it means item activity has added a new item to the database. Therefore, we must reinitialize the data set using the initialize dataset method. Next, we must let the adapter know that the dataset has changed using the adapter.notifyDataSetChanged method. If you run the app now, and add a new item, you should be able to see it instantly. You now know how to create and use a dataset for a recyclerViews adapter. Thanks for watching.

Back to the top