Android ListView

Overview

We will learn about how to show lists of objects in an android app using the listview widget.


Pre Requisites

Finish the Android Intro project before trying this project.

Concepts

In this section, we will explain how a ListView works in Android, ListView is a widget that is used to show collections of objects (eg: List of contacts or list of countries etc). ListView is a container widget. For each item in the list, the ListView widget will contain another View that represents the item to be shown. This item view can be made up of regular widgets such as textview, imageview, buttons etc.

Following is the approximate flow sequence for a listview to work:

  1. Define an activity and place a listview in the activity's layout file.
  2. Define another layout file that holds the layout for the items that will be shown inside the list. This is what we refer to as the item layout xml file in the above diagram.
  3. Override the ArrayAdapter class (or any other adapter class) and provide your implementation for GetView() method. This method will return the View object for each item in the listview. The View returned by this method will match the layout defined in the item layout xml.
  4. Create an instance of your adapter class - You need to pass the id of the item layout xml file as well as the id of a textview widget contained in the item layout as arguments to the constructor of the adapter class.
  5. Call setAdapter on the listview to bind the listview to your adapter instance.

The Tutorial section will walk you through an example.

ListView Performance

Since lists can show thousands of data items, it is important to understand performance aspects of listview.

 

 

Setup

Follow the Getting Started on Android document to get your environment up and running for developing Android apps


Tutorial

 Lets walk through an example. Suppose you want to show a list of contacts. Each contact will have the name and an image shown.

 

For simplicity, we are just showing a Star image next to each contact name, but you can easily extend the example to show a contact's photo for example. The following are the various components needed to build the above android app:

 

 

In both implementations, we call the super.getView() method to get the View associated with the adapter and let the base ArrayAdapter handle inflating the view/handle view recycling. The code expects this to be a view with 2 widgets (TextView and ImageView). We set the value of TextView to hold the contact's FullName. The code sets the ImageView to the btn_star image resource to show a star image next to the contact.

You can run the ImageViewTutorialActivity in Android Studio to try out the tutorial yourself.

Project Activities
 

Here are the activities you will work through in this project.

Simple Contact List

In this activity, you will build an app that shows a list of contacts as shown below. The list shows only 1 column/item (single item list)

 

 2 Item List Views

In this project, you will build an app that shows 2 items (fullname and company) for every row in the list. An example screenshot is shown below.

 Sorting in ListViews

In ths project, you will enahnce the contact list app built in the previous step by adding 2 buttons to sort the list in ascending/descending order as shown below.

 

onClick of ascendingButton, you need to implement an onClickListener that will sort the contacts by FullName in ascending order as shown below.

Similarly, onClick of descendingButton, sort and display the contacts list in descending order by FullName as shown below.

Pagination

When you have to deal with large amounts of data, a typical practice is to show a small number of items per page and provide a Next button to fetch more data. This approach is called pagination. In this project, you will implement pagination on a listview and display a list of countries.

A file containing country names is located at app\src\main\assets\countriesoftheworld.txt file. It contains 13 country names.

You have to build an app that shows a list view of 5 countries at a time as shown below.

 

 on click of the Next button, the app will fetch the next 5 countries and display them as shown below

 

 

 When the last page is reached, the next button should be disabled as shown below.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This Web Page was Built with PageBreeze Free HTML Editor