pr0methium
pr0methium

Reputation: 41

Manually populate a ListView

Is there a way to manually populate a ListView, that has more-than one column, using multiple arrays? I need to build out a section of a layout, and I want to use a ListView but I need to populate several View items with data. Since it's not coming from a database, I don't want to use a cursor, I just want to use several parallel arrays. Is this possible?

Upvotes: 4

Views: 1350

Answers (3)

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9755

Did you try using this as main list adapter? -> https://github.com/commonsguy/cwac-thumbnail

CommonsGuy has nice examples about that, and easy to customize to generate ListView with dynamic items.

Upvotes: 0

tlvb
tlvb

Reputation: 11

Depending on wether I have interpreted your problem correctly, this should be possible to do with an adapter:

You could extend BaseAdaptor, overriding getView() and have it return some ViewGroup containing the widgets representing the data from the different lists.

Subclassing a ViewGroup of choice to build a compound view that is easier to recycle:

public class DoubleText extends LinearLayout {
    TextView t1, t2;
    public DoubleText(Context c) {
        super(c);
        t1 = new TextView(c);
        t2 = new TextView(c);
        this.addView(t1, 0);
        this.addView(t2, 1);
    }
    public void setText(String s1, String s2) {
        t1.setText(s1);
        t2.setText(s2);
    }
}

Subclassing BaseAdapter, however not everyithing needed is shown, there is more, but it should be easier to figure out, as this where the bigger differences are compared to other examples regarding using adapters:

public class DoubleAdaptor extends BaseAdaptor {
    List<String> lista, listb; // <- these are your paralell arrays, they and the
    Context c;                 // context need to be initialized by some method
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DoubleText recycled = (DoubleText) convertView;
        if (recycled == null) {
            recycled = new Double(context);
        recycled.setText(lista.get(position), listb.get(position));
        return recycled;
    }
}

Also, you don't have to have the same view type on all rows, i.e. override getViewTypeCount() to give android the upper limit of the number of different views types to use and getItemViewType(int position) to tell android which type of view to recycle for that particular row.

Edit: I would recommend checking out the android developer docs on the Adapter (interface) and BaseAdapter (abstract class) and ListAdapter (interface), also I forgot to tell you, the ListView has a setAdapter() that might be of interest.

Upvotes: 1

Dan Breslau
Dan Breslau

Reputation: 11522

It can be done; the question is how ;-) If you really want to use multiple, parallel arrays, then you may have no choice but to subclass BaseAdapter. If you are using (or can use) an array of objects, you can do a somewhat simpler subclassing from ArrayAdapter. In the latter case, I think the only method that you need to override is getView.

Upvotes: 0

Related Questions