views:

54

answers:

1

My code works like this to list all items in my String array - itemsarray

setListAdapter(new ArrayAdapter<String>(this, R.layout.row,
        R.id.label, itemsarray));

However, I know by this call that I only want to list the first X number of items from itemsarray. How can I load only the first X items form itemsarray into the ListAdapter?

+1  A: 

There's no way to do it automatically... you will have to do it manually. You have two alternatives:

// the easy one:
ArrayList<String> someItems = new ArrayList<String>();
for(String element : itemsarray) // add the first 5 elements
    someItems.add(element);
setListAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.label, someItems));

Or you can create a subclass of ArrayAdapter and override the getCount method returning X. It will make the list think it just have X elements to show.

Cristian
I already was using a subclass for ArrayAdapter and adding @Override public int getCount() { return X; } was exactly what the doctor ordered. Thanks again Cristian!
Tim Wayne