Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set maximum number of rows for a ListView

Tags:

java

android

This is a question about Android programming. I need to limit the maximum number of visible rows in my ListView.

In onCreate method I have the next code for my ListView:

listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListOfElements));

In onPostExecute method I have the next adapter:

ArrayAdapter<String> adapter = (ArrayAdapter<String>) arrayListOfElements.getAdapter();
adapter.notifyDataSetChanged();

So I need to set a maximum number of rows for my adapter, I don't care if the ListView will be scrollable or not. Any idea?

like image 592
Roman Bas Avatar asked Dec 07 '25 09:12

Roman Bas


1 Answers

Do this in your base adapter (mList is your list of items to show in the listview):

public int getCount() {
    if (mList != null) {
        return Math.min(mList.size(), YOU_MAX_VALUE);
    } else {
        return 0;
    }
}
like image 128
Francesc Avatar answered Dec 09 '25 21:12

Francesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!