user1875797
user1875797

Reputation:

Android - How to add multiple arrays to listview

I've put data from my database into four arrays but I want to bind it to my listview. Unsure how to bind them to the listview with multiple arrays though.

My listview is four columns, so something like this (I'm aware this is wrong);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, 
            R.layout.myactivity_four_column, meal[i], calories[i], fat[i], protein[i]);

Upvotes: 0

Views: 1329

Answers (2)

Viral Thakker
Viral Thakker

Reputation: 547

You can use Object in place of four Arrays.

You can make Entity class (which has getter and setters) for it.. and then You can override the toString() method and return any variable of entity

and then u can make array adapter of that object and print the specific values. If you want to print all four values then u can return the string which contains all four value by appending to one another and return it to toString() method.. like,

List<Object> objectList=getListFromDatabase();

 ArrayAdapter arrayAdapter = new ArrayAdapter(this, 
        R.layout.myactivity_four_column,objectList);

listView.setAdapter(arrayAdapter);

Upvotes: 0

iTech
iTech

Reputation: 18430

You just need to define an ArrayList and pass it to the ArrayAdapter, e.g.:

ArrayList<String> myList = new ArrayList<String>();
myList.addAll(Arrays.asList(new String[] {meal[i], calories[i], fat[i], protein[i]}));

Then bind with myList

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.myactivity_four_column, myList);

Upvotes: 1

Related Questions