Dynamic RecyclerView With Non-Defined Data Structure (From Unknown CSV Data Structure)

Tarek Ben Driss
Feb 16 · 4 min read

RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they’re needed.

RecyclerView example

Generally, we use RecyclerView with a defined data structure just like in the example mentioned above. There, we can notice that the shown data could be grouped into an item of a title and an image url.

But suppose we need to show a list of unknown data structures, and let’s get the example of an app that should display the content of a CSV file without knowing its content, and that content can changes its structure anytime.

Let’s get 3 CSV files with different contents and which should be displayed by the same RecyclerView, and as known from csv files, the first line is the key and the other lines represent the values :

  • The first file will contain the name and the age of some persons.
  • The second one will contain the stats of some apps, the name, the number of downloads and the number of users.
  • And the last one will contain the first name and the last name of a customer, the birth of date and the number of issues.

To start, the 3 files should be placed under res -> raw

Note that we are using data-binding in this project, so just add this line into your app’s build.gradle :

dataBinding {
enabled = true

RecyclerView Item :

Our dynamic RecyclerView item will only contain an empty LinearLayout inside a Cardview, we will add our data dynamically in this LinearLyout.

Almost all the work will be done in the Adapter.

Model :

The RecyclerView’s model “StandardModel” is a simple class that contains an ArrayList of Strings. The first item of that list will contain the CSV key, and the other items represent the values. A list of StandardModel will be generated in the MainActivity and transferred to the Adapter.

MainActivity :

In the MainActivity, we will parse any CSV file we want and display it’s content dynamically in the RecyclerView.

The buttons issuesBtn , usersBtn and appStatsBtn allows changing between the CSV files.

We override the function doInBackground of the class ReadCsvAsyncTask to get the CSV titles and contents.
The titles are obtained from the first line of the file and the values from the rest of the lines. Columns are separated by the comma, that’s why we split the list by split(“,”)

After parsing the whole file, the data we got will be transferred to the adapter so it can be displayed in the RecyclerView.

Adapter :

As mentioned before, here will be done all the work.

As we can notice in the CsvContentAdapter, for each item of the StandardModel list we create a new TextView filled with the title and the value.
The title, obtained from the titles list, will be bold using HTML tag <b> </b>.
Finally, we add the created TextView to the LinearLayout previously defined.

Hence, after browsing all the model’s list, the unknow CSV file struture will be displayed in the RecyclerView just like the picture below.

Hope I described the project well, otherwise you can find the whole project on my github:

Sharing (knowledge) is caring 😊 Thanks for reading this article. Be sure to clap or recommend this article if you found it helpful. It means a lot to me.

If you need any help, join me on LinkedIn and GitHub.

The Startup

Get smarter at building your thing. Join The Startup’s +729K followers.