Create a new class with 3 variables to hold 3 strings.
Create an
ArrayList
of your new class and populate it with 3 strings for each object. (This is instead of 3 separateArrayList
s)Create a custom
ArrayAdapter
which will allow you to display those 3 strings and use it for yourListView
.
Android - How to populate ListView from 3 ArrayList
-
20-06-2023 - |
Question
I am doing some android development and stuck at a point.
I need to populate a ListView and the data is in 3 different ArrayList of String.
Also i have customised a single list view item(it has three textviews) so that the data from those three different list must go at different textviews.
Any way i can populate this ListView ?
Solution
OTHER TIPS
Assuming these 3 lists are of same size and their elements are co-related, then set one of the list to the adapter, then populate all the 3 list data in getView(...)
That should do
For a cleaner approach please consolidate all the 3 different data from the list into 1 class
structure and a list
of this class.
From what I could understand, you do not need three different ArrayLists for three different data types.
A better (and possibly the only proper approach) would be to store those data in a POJO (Pure old java object) and create an ArrayList of this POJO.
Furthermore, you'll also have to create a custom ArrayAdapter
that uses your ArrayList as its underlying data set. Here's a nice tutorial for this:
Then simply pass this ArrayList to your custom ArrayAdapter
for your ListView
.