I am trying to pass an ArrayList<int[]> to an array adapter. Each int array contains 6 values and I would like those values to appear on each row of a listview. Is there a way to do this using the standard array adapter or would I need to create a custom adapter for this?
1 Answer
You will have to do something differently.
As the docs for ArrayAdapter note:
However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.
To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.
Some options:
Convert your list to one more suited for display:
ArrayList<String> newList = new ArrayList<String>(); for (int[] i : yourList) { newList.add(Integer.toString(i[0]) + ...) };Create a custom adapter more suited to your list:
public class MyAdapter extends ArrayAdapter { @Override public View getView (int position, View convertView, ViewGroup parent) { int[] i = getItem(position); // Inflate an appropriate layout and populate it with the ints in i }
ArrayList<int[]>toArrayAdapter, it is impossible to display the array's value to the list without any modification sinceArrayAdapterwill usetoString()to display the referenced items.