Want to load part of an array in android ListAdapter

My code works to list all the elements in my String array - itemsarray

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

However, I know from this call that I only want to list the first X number of items from itemarray. How can I load only the first X elements from an itemarray in a ListAdapter?

+3
source share
1 answer

There is no way to do this 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 subclass ArrayAdapterand override the method getCountthat returns X. It will make the list think that it simply displays X elements.

+1
source

Source: https://habr.com/ru/post/1753822/


All Articles