0

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
  • 1
    You need to create a custom adapter. While it's possible to pass an ArrayList<int[]> to ArrayAdapter, it is impossible to display the array's value to the list without any modification since ArrayAdapter will use toString() to display the referenced items. Commented Nov 5, 2013 at 1:11

1 Answer 1

1

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
        }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.