Iiro Krankka
Iiro Krankka

Reputation: 5239

Android: multiple Arrays in custom ArrayAdapter

I would like to pass multiple arrays for my custom ArrayAdapter. Here's my arrays and what I want to do:

String[] names = new String[]{ "One", "two", "three" };

String[] texts = new String[]{ "Bacon", "Eggs", "Cheese" };

Customadapter ap = new Customadapter(this, names, texts);
setListAdapter(ap);

and here's my custom ArrayAdapter:

public class Customadapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] names;

    public Customadapter(Activity context, String[] names) {
        super(context, R.layout.row, names);
        this.context = context;
        this.names = names;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.row, null, true);
        TextView tw1 = (TextView) rowView.findViewById(R.id.label);
        TextView tw2 = (TextView) rowView.findViewById(R.id.label2);

        String text = names[position];
        tw2.setText(text);

        return rowView;

    }

}

Upvotes: 2

Views: 3816

Answers (3)

Ron
Ron

Reputation: 24235

Instead of having a ArrayAdapter<Strings> you need to create a adapter of ArrayAdapter<HashMap<String,String>>

Upvotes: 1

MrJre
MrJre

Reputation: 7161

extend BaseAdapter instead of ArrayAdapter for starters, then adapt the constructor so that it takes the texts string as well such that you can store it in the Adapter. That should do the trick mostly. There are some methods needing overriding afaik, but its there in the docs.

Upvotes: 0

Dan S
Dan S

Reputation: 9189

Try using a SimpleAdapter instead.

Upvotes: 0

Related Questions