3

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.

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to extend it, since I need to have an id accociated with each entry on the list, so that selecting items can be updated against a database or file. Any good, to-the-point pointers on how to extend it?
I chose option 1: a 2D array of the objects I want to display, along with transformation logic to convert it to a 1D array including section header objects in between. Note: this also means every action requires you to update both structures and frequently make transformations if you want to keep working with the 2D array. I would recommend taking a crack at option 2
2

I suggest, instead of creating a 2d array, create a certain Model that suits your needs. In this case:

class Model {
    private String mText1;
    private String mText2;

   //getters and setters

}

And create a custom ArrayAdapter (a class which extends ArrayAdapter<Model>).

That way it would be easier to add extra information to your ListView with minor modifications; e.g. adding an image.

Check out this tutorial for a detailed explanation.

Comments

1

Creating a model, like Benito mentioned above, as an inner class always works well.
Good way to store any number of items.

public class ActivityClass extends Activity {
    ...
    ArrayList<ValuesModel> listViewValues = new ArrayList<ValuesModel>();
    listViewValues.add(new ValuesModel("row title", "row details"));

    ListViewAdapter listAdapter = new ListViewAdapter(this, listViewValues);
    ((ListView) findViewById(android.R.id.list)).setAdapter(listAdapter);
    ...

    public class ValuesModel {
        private String rowTitle;
        private String rowDetails;

        public ValuesModel(String rowTitle, String rowDetails) {
            this.rowTitle = rowTitle;
            this.rowDetails = rowDetails;
        }
        public String getRowTitle() {
            return rowTitle;
        }
        public String getRowDetails() {
            return rowDetails();
        }
}

Then inside of your list adapter,

public class ListViewAdapter extends ArrayAdapter<ActivityClass.ValuesModel> {
    private ArrayList<ActivityClass.ValuesModel> mValues;
    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        //here whenever you need to retrieve your values, just say:  
        //  mValues.get(position).getRowTitle();
        //  mValues.get(position).getRowDetails();
        //if you use a viewholder pattern, you can do this:
        viewHolder.rowTitle = (TextView) convertView.findViewById(R.id.row_title_textview);  
        viewHolder.rowTitle.setText(mValues.get(position).getRowTitle());
        ...
    }
}

Comments

0

You can simply extend ArrayAdapter. All you need is to override the getView() to bind the element[2]. probably something like this:

public class MyArrayAdapter extends ArrayAdapter<String[]> {
    public View getView(int position, View convertView, ViewGroup parent) {
        ((TextView) super.getView()).setText(getItem()[2]);
    }
}

1 Comment

Sorry, I don't understand where getView() enters the picture here?

Your Answer

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