The purpose of the ListView is to contain a long, vertical list of similar items. You probably already knew that part, but it's important to establish the basics.
You might ask “Why use ListViews when I’ve got linearlayouts?” and that is a very good question. In many ways, ListViews are very similar in purpose to the linearlayout.
The issue with linearlayout is that they keep every view they contain in memory at the same time. If your linearlayout has 10 things to display, it needs 10 views. If you have 100 things to display, it needs to contain 100 views. 50? 1000? Linearlayouts quickly get bogged down under those kinds of numbers.
Well, someone clever realized that, even though you may have 1000 items to display, only 10 or so views are visible at the same time. Why not reuse those 10 views to display all 1000 items? As views go off the top of the screen, they could be recycled and moved to the bottom to display different content.
This concept became the ListView.
The listview contains most of the logic that makes that process work; however, there is still some logic left that you need to implement. The Adapter is the object that contains that logic.
Barring some very special circumstances, I always extend BaseAdapter to create my adapters.
Your adapter should be a separate class, in its own file. You can put it in your activity as a child class, but that can lead to bad habits, so I suggest getting started with it in its own class.
When you extend BaseAdapter in your own custom adapter (which is what you should do), you must override a handful of methods. getCount, getItem, getItemId and getView. Once you’ve given your listView a new instance of your adapter, it can call these methods to get the information it needs to display your content.
You will also need to create a way to give the adapter the content you want to display. If you’ve got a list of integers you want to display, then your CustomAdapter needs to contain a List<Integer> myStuff, as well as a setter for that List.
Additionally, your adapter will need a context/inflater, so pass that in through your constructor and hold onto a reference to it.
Let’s keep the explanations for these methods simple.
getCount() should return the number of items you want to display. If you’ve got 10 ints in your list, it should return 10. usually, this just ends up returning myStuff.size().
getItem(int n) should return the item you want to display at ListView position n. This usually ends up just being myStuff.get(n)
getItemId(int position) - don’t worry about this one, you can just return position
getView is where the fun stuff happens. This takes in 3 parameters, position, convertView, and parent. Position, as you can probably guess, is the position of your item.
Remember before how ListViews recycle views that have gone off the screen? convertView is that view.
it might be null, which would mean that there are no views available to recycle, so you have to make one. get your layoutinflater that you saved earlier (or get one from your context), and inflate a view. You could also just say “new TextView” or whatever you like and use that.
If it’s not null, then you can cast it to whatever view you made earlier and carry on, since it’s ready to display some content.
“parent,” the third parameter, is your listView, which you’ll use to inflate a new view when convertView is null. (Remember, when you inflate views with a layoutinflater, you should always tell the inflater what you’re inflating that view into. Android does some stuff with that information, and things get weird if you don’t)
You might have heard some people talk about viewholders. They’re useful, but I’d worry about them after you’ve got the basics down.
So, the final question remains, how do I actually put new content in my adapter? It’s actually really simple. Remember the setter we made earlier for our list of content? Just call that, then call notifyDatasetChanged on the adapter. That last step is very important; best case, your ListView items won’t change to reflect your new content, worst case, your app may crash.
Listviews can do a lot of other things, and some of them require that you do more stuff with the adapter, but this should get you going.