Bohemian
Bohemian

Reputation: 6067

How to pass more than one array to a listview?

I am using a ListView to display items. Currently I am passing a String array of items to it. But I want to pass one more array and display its items along with the items of the first array; that is, somehow having two lines of text.

How can I do that?

Upvotes: 1

Views: 688

Answers (3)

Samuh
Samuh

Reputation: 36484

Since both the arrays contain data to be displayed in a row of a listview, IMO it is logical and fair to believe that they are related in some manner.
You can create a class called RowData and have a String(data type of first array) and Object(or datatype of second array) as its members. You can then pass an array of Rowdata type to the list view.
For instance, if you want to pass: String[] and Date[] to the listView you will pass RowData[] now, where Rowdata is defined as:

class RowData{ 
String dataItem1; 
Date dataItem2;
}

Upvotes: 1

Ramps
Ramps

Reputation: 5288

You could also make a custom Adapter for your list and use custom class as type of items of that adapter. Something like this:


class CustomAdapter extends ArrayAdapter < RowData > {
   ...
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        ...
        }
}

class RowData {
   String line1;
   String line2
   RowData(String l1, String l2){line1=l1;line2=l2;}
}

Regards!

Upvotes: 3

Aaron Digulla
Aaron Digulla

Reputation: 328556

Use a table or format the items of the various arrays into a new array.

Upvotes: 0

Related Questions