Recently I wanted to show cards in my application which gets updated according to the month I have added in my list. Inside the card, I wanted to show the list of items that I have added for that month. Being a beginner in android, it took me a long time to figure this out so I decided to write a "how to" post on the same. I will be explaining the main files, complete project is available here.
Let's get started
I have created a simple application as an example
In the above screen, there is an option to add name and a category to which it belongs. For selection of a single category, I have implemented a spinner inside this fragment. On the home page, a card will be generated according to the category added and it will show the name of the category and the respective list of items belonging to that category. Floating action button can be used to add more items to the list.
I have used MVVM architecture and single activity multiple fragments pattern to implement this app. There are two parts to this. One is on adding cards according to the category added to the database and the other one is on how to insert list inside an existing list.
importandroidx.lifecycle.LiveDataimportandroidx.room.Daoimportandroidx.room.Query@DaointerfaceFavouritesListDao{@Query("SELECT f1.category, (SELECT f2.name FROM favourites as f2 WHERE f1.category = f2.category) FROM favourites as f1 GROUP BY category")fungetData():LiveData<List<Category>>}
Enter fullscreen mode Exit fullscreen mode
The above sqlite query is responsible for displaying cards according to the category inserted. The GROUP BY clause will group the data with respect to category. This query will fetch data from the database. Following data class is being used to store the results obtained by the above query.
Results obtained from this query present inside the above sqlite query will be stored inside 'children' list. This will be added to the listView inside cardView.
Here is the itemAdapter class:
classItemAdapter(valcontext:Context):ListAdapter<Category,ItemAdapter.ViewHolder>(DiffCallback()){overridefunonCreateViewHolder(parent:ViewGroup,viewType:Int):ViewHolder{valitemLayout=LayoutInflater.from(parent.context).inflate(R.layout.card_item,parent,false)returnViewHolder(itemLayout)}overridefunonBindViewHolder(holder:ViewHolder,position:Int){holder.bind(getItem(position))}innerclassViewHolder(overridevalcontainerView:View):RecyclerView.ViewHolder(containerView),LayoutContainer{funbind(item:Category){with(item){when(item.category){Categories.Movies.ordinal->{category_name.text=Categories.Movies.toString()}Categories.Sports.ordinal->{category_name.text=Categories.Sports.toString()}Categories.Fruits.ordinal->{category_name.text=Categories.Fruits.toString()}else->category_name.text=Categories.Vegetables.toString()}// Setting up list viewvallist:List<String>=item.children.map{it.name}valadapter=ArrayAdapter(context,android.R.layout.simple_list_item_1,list)item_list.adapter=adapter}}}}classDiffCallback:DiffUtil.ItemCallback<Category>(){overridefunareItemsTheSame(oldItem:Category,newItem:Category):Boolean{returnoldItem.category==newItem.category}overridefunareContentsTheSame(oldItem:Category,newItem:Category):Boolean{returnoldItem==newItem}}
Enter fullscreen mode Exit fullscreen mode
This code segment inside the above code is used to set up listView inside card_item (cardView)
// Setting up list viewvallist:List<String>=item.children.map{it.name}valadapter=ArrayAdapter(context,android.R.layout.simple_list_item_1,list)item_list.adapter=adapter
Enter fullscreen mode Exit fullscreen mode
To set-up the recyclerView inside home fragment, add this code
To checkout how items are getting inserted in the database and the structure of the app, here is the github link from where you can download this project