Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ListView, ArrayAdapter and 2-d array

I need to display contents (most likely just one index) from a 2-d string array in a listview.

I can create an arrayadapter for a 1-d array like this:

String[] values ... blah-blah

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
    android.R.layout.simple_list_item_1, android.R.id.text1, values);

listView.setAdapter(adapter); 

This works just dandy. However, if I do this:

String[][] values ... blah-blah

ArrayAdapter<String[]> adapter = new ArrayAdapter<String[]>(this,  
    android.R.layout.simple_list_item_1, android.R.id.text1, values);

listView.setAdapter(adapter);

This, naturally, will display the arrays in the array, so to speak, and not the contents.

The question is, how to I display f.ex. the first or second index of the 2-d array in the ListView?

In detail:

2-d array contents:

3,03-09-2012,text,moretext
2,03-09-2012,text,moretext
1,03-09-2012,text,moretext

I'd like to display f.ex. index[i][2] in the list.

I don't necessarily need to display all the indexes of each array, but I need to have access to them. Since each array contains several entries, among others an ID, I cannot just convert the information I want to display to a 1-d array, since then I'd lose the ID of each array.


1 Answers

There are two solutions for you to choose from:

  1. Create a method that transforms a 2D array to a simple array, using your own transfomation logic.
  2. Extend the BaseAdapter class to create your own implementation, where you can have a 2D array as the underlying data and display its contents in a way you want.

Hope this helps.

like image 70
Egor Avatar answered Dec 06 '25 05:12

Egor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!