ListView was added to Android 2.0 back in 2009, which made many new life for Android developers. A lot of apps became easy to implement and the time for developing such an app was reduced by a huge factor. In this article, I want to give you a detailed explanation about what ListView is, how this works under the hood and why we should actually use it.
As already mentioned above ListView allows us to develop applications faster with its library functions given by Google especially when displaying lists of items. We don’t have to write all those functions like scrolling through all rows when clicked on a list item or adding new items and so forth ourselves anymore. We can just use all these functions provided by Google. This allows us to build more robust and smooth applications in less time.
But let’s have a detailed look at how this works under the hood before we start with our first application, which should display a list of persons. And as always for my articles I will focus on Android 4.1 Jelly Bean since it was released not that long ago and still represents most devices you see on the market today.
The ListView is part of Android’s Views Library which makes it available inside your Activity. It also has its own lifecycle and therefore needs to be added to an appropriate place within your Activity. Since there are many cases where developers change Activities, which contain ListViews the first decision for us is to store our ListView in a separate class, which we can use throughout all activites.
Furthermore the creation of a list requires not only that we provide an Adapter . Since the ListView is created by extending it to our own class and therefore changing some important methods like onItemClicked() , onListItemClickListener() , onItemTouchListener() and a lot more we better extend the ListView class instead of just using it. So that way we can also overwrite these functions with our own code, which can be reused throughout all our activities.