How to create a list of items in Android
5 minute(s) read
Published on: Sep 16, 2021
Updated on: Dec 14, 2021
In designing developed applications ( in Slovak: navrhovanie vyvinutých aplikácií ), lists can have many applications, and it can be said that lists are one of the most important elements of advanced applications. For example, lists displaying all financial transactions for users in financial apps can be handy.
How to create lists in apps?
To display lists in applications and create information regularly, we can use RecyclerView or CardView on Android operating systems.
RecyclerView has some advantages over ListView as follows:
- It is worth knowing that RecyclerView is optimal, and its memory can be better managed. When using RecyclerView, not all items are included in the list, and only items on the screen can be listed.
- RecyclerView is also easier to manage and can be easily managed. Of course, it should be noted that it is easy to change the arrangement and create many columns if necessary.
- To beautify, it is possible to add animation in RecyclerViews and to beautify it, you can add different animations or make some necessary settings for them.
RecyclerView can display scrollable items and can easily be scrolled up and down the list. This means that there is no limit to the number of things you can list in RecyclerView. This feature can be used to display items as a list, including messages in messengers, emails, contacts in the mobile phone book, a list of cities, and their weather data.
Using this feature to display items as a list in emails and messages, in addition to the contact's name, the short and initial part of the message is also shown.
1- To be able to use this feature and to be able to display items and data as a list, we must first design and specify a general format for displaying items in lists.
2- Then, we must specify all the information we want to display to the user.
This means that there is the possibility of personalization in the RecyclerView feature and option. We can easily use all items such as text in combination with images, animations, videos, etc. All this data is also transferred to the RecyclerView through the database for display.
3- To do this, we create a new project and select a custom name. The custom name chosen in this article for this project is RecyclerView.
4- We also create an Empty Activity.
5- To complete this project and use this feature, we must first add the related libraries to the project. The related library is also a support library.
6- We can add the library to the project manually by going to File- Project Structure- Modules (app)- Dependencies> +- Library Dependency.
7- In addition to that method, we can add the desired library by entering some codes to the project. Like the following:
8- After the project has been successfully synced, we add a widget or component of the RecyclerView type to the main active layer of the project, which is activity_main.xml.
9- Then, we add RecyclerView to the project.
10- The codes that must be entered in MainActivity.java are as follows:
In the code above, we use the so-called Model class to manage widgets and views and communicate with Java operators. Of course, it should be noted that the use of this class is not mandatory, but to have good management and control, we can use this class.
There are methods such as Getter and Setter in this class that can be used to quantify.
In general, we want all the data and items in RecyclerView to have a profile picture, name, text, message, and even the date they were sent.
11- To do this, we define four variables until one of them displays the profile picture, text, name, and submission date.
12- We add construction methods such as Constructor and Getter & Setter to the project.
13- We add a file named list_item to the project layout directory.
In the code above, this layer includes an ImageView and three TextViews. We also insert the person's profile picture in the drawable directory.
14- The output of list_item.xml is such that it displays the items as a list and displays the profile image next to it.
15- In the following steps, to view or send the received data, I will create a class called Adapter.
16- Then, we add another class called ItemAdapter to the project.
We add an internal class with a custom name, such as MyViewHolder, to the ItemAdapter class in the code above. Of course, this class must inherit from ViewHolder.
17- Inside MyViewHolder, we need to define the views.
18- Then, an error is displayed for us in the MyViewHolder class, which when we click on it, we will see that asks us to add the constructor method to the class.
19- In the next step, we must connect the views in the constructor method, which is related to the variable.
20- After we build the ViewHolder, the ItemAdapter class from RecyclerView.The adapter should be able to inherit.
21- Finally, the class should be completed as follows:
22- If we see an error, we can also fix the error by completing the class as follows:
In all the steps we have included in this article, we have designed the app so that it can display all the data and items on the page in a list and regularly, along with the profile picture, name, and details for the user.Click here to create a mobile app