Dynamic RecyclerView With Non-Defined Data Structure (From Unknown CSV Data Structure)
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.
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 :
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.
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.
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.
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:
I’ve written some other Android-related content, and if you liked what you read here, you’ll probably also enjoy this:
“Continuous Integration/Delivery” for Android with GitHub Actions — Part 1
Dear developers, In this article I will show you how to automate the test execution and build of your Android app…
Implement shortcuts, the Android ‘iPhone 3D Touch’ equivalent
In this article, we are going to develop a simple call and opening camera Android app and integrate static “App…