Tim Wayne
Tim Wayne

Reputation: 2265

Want to load part of an array into android ListAdapter

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?

Upvotes: 3

Views: 698

Answers (1)

Cristian
Cristian

Reputation: 200160

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.

Upvotes: 1

Related Questions