11

I'm new to Android programming. I wonder how many items a ListView can store? I search in docs but they don't talk about this. What if I put a lot (maybe 10k) items into a ListAdapter, will it affect to the performance?

Cheers, MK.

Minh Kiet
  • 131
  • 1
  • 2
  • 5

5 Answers5

8

The ListView is virtualized in Android. Practically speaking, that means that there's no actual limit for the number of elements inside it. You can put millions of rows inside the list and it'll only allocate memory for the currently visible ones (or a few more tops).

Check out the dozens of tutorials regarding writing a custom Adapter class for an AdapterView (ListView extends that). Also check out the Google I/O 2010 session on ListViews; it's really useful: here

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101
  • 1
    We are keeping this 10k items in an arraylist for the list adapter. Whether it leads to any memory issue? – user1767260 Feb 13 '14 at 03:50
  • That entirely depends on how much memory each of these items consume and how much heap space the devices have that you target. Having said that, you probably need some windowing mechanism with item counts of this magnitude. – Zsombor Erdődy-Nagy May 13 '15 at 04:26
5

There's no limit as the ListView only renders items when they come into view, and so only cares about the data for the ListView when it comes to render the item (though it needs to know the quantity of items to render the scrollbar correctly)

The Google IO video really is great for learning about ListView http://www.youtube.com/watch?v=wDBM6wVEO70

That said, I'd ask whether you SHOULD load that many, as clearly the user cannot look at them all and scrolling around a ListView with that many items will be very tedious. If it was me I'd be asking some questions:

  • Does the list need to show them ALL initially? Can it just show the most relevant set? The nearest/biggest/smallest/best/etc
  • Rather than load them all at once, can you load blocks in pages of items? So for example you load 10-100 initially, and when the user gets to the bottom show "Loading more..." with a progress spinning icon, and pull more in, then the user can choose how many to load, and how much scrolling they are prepared to do
  • Should you be building a UI to filter the items so there's never a need for them to look at 10,000?

More on ListView http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ http://developer.android.com/guide/topics/ui/binding.html http://www.androidguys.com/2008/07/14/fancy-listviews-part-one/

Ollie C
  • 28,313
  • 34
  • 134
  • 217
3

Integer.MAX_VALUE since most of its functions will break as they rely on int position.

Monstieur
  • 7,992
  • 10
  • 51
  • 77
3

I created a ListView and used Integer.MAX_VALUE as the number returned to the adapter for the number of items in the list. This had no effect on the performance of the ListView even though it was holding about 2 billion items.

I think 10k will work :P

Check out my answer here to see in detail what I mean : How to create a closed (circular) ListView?

Dawson
  • 4,391
  • 2
  • 24
  • 33
-1

You have a memory limit (which is device specific). As long as you don't exhaust your memory limit, you can store as many items as you wish. There are ADT tools that you can use to monitor how much memory your application is using at any moment.

Shade
  • 9,936
  • 5
  • 60
  • 85