1. Introduction

This post will walk you through building simple and customized ListView in Android using different Android adapters.

Scrollview is ideal for screens where scrolling is required, but it is not efficient when scroll view is used to render a larger data set. Instead you can use specialized adapter views like ListView, GridView and Recycler View (Introduced in Android Lollipop) for long lists.

ListView is an Android ViewGroup, used extensively to display the collection of data in vertical scrollable rows.

The list items are automatically inserted to the list using an Adapter and Adapter pulls data from data source source such as an array, cursor, etc.

2. Android Adapter

Adapter is acts as a bridge between data source and adapter views such as ListView, GridView. Adapter iterates through the data set from beginning till the end and generate Views for each item in the list.

Android SDK provides three different Adapter implementation, that includes ArrayAdapter, CursorAdapter and SimpleAdapter. An ArrayAdapter expects an Array or an List as input, while CursorAdapter accepts the instance of Cursor and SimpleAdapter maps the static data defined in the resources. The type of adapter that suits your app need is purely based on the input data type.

The BaseAdapter is the generic implementation for all of the three adapter types and can be used for ListView, GridView or for Spinners.

You may directly use ArrayAdapter by passing array as input or create your own customized class by extending BaseAdapter.

The image above, provides idea of customizable list views can be done using adapters.

3. ListView Using ArrayAdapter

The simplest way for building list view is by using ArrayAdapter. Following are some of the steps used to implement simple ListView using array adapter.

First step towards building list view is to identify the input data, which you want to display in list. In this example, we will be using a static array of strings.

4.4. Hooking Up to Activity

Well, we are almost ready. Let us put all of them together and hook it up to the activity. Following code snippet depicts the activity class (MainActivity.java), where we initialize Adapter and hook it up to ListView.

5. Styling Android ListView

Like any other view, ListView in Android can customized by color, background, selection color, etc. In this section of tutorial, we will learn how to customize an Android ListView.

5.1. Changing ListView Selection Color

ListView default selection color can be changed using selectors. Selectors enables to decide how list row will visually be represented based on different states. Let us change the background and text color while list row is in pressed state.

Create a new file named list_color_selector.xml inside your drawable folder and paste the following code snippets. This selector style will be used to change the background color when list row is pressed.

Hi, Nice tutorial you wrote. Do you have a clue on how to set a default background colour for each list item? So, at the start, all list items have a yellow background. Regards, J

PS: your last GIthub link is not correct.

http://javatechig.com/ JavaTechig

You can set android:background=”#ff0000″ property to relative layout in list_row_layout.xml.

Github links are updated. Thanks for pointing out.

tmj1706

Great tutorial. Thanx

http://javatechig.com/ javatechig

all java code should be in src folder, xml layouts in layout folder and resources files in drawable.

Didi

Hi, I followed your example for selecting in a listview and it works perfectly, but what if I want to select more than 1 item, I tried adding android:choiceMode=”multipleChoice” in my listView, but it does not work

i’m sorry i was so vague. i know that, but what i was asking was, if it possible to to that through the dynamic part of your example?

thanks again

http://javatechig.com/ javatechig

Sure, it is quite possible. For that you just need to call the methods like

holder.headlineView.setText("Your text goes here");

You can set attributes by calling all available methods of TextView.

Matt Fuller

Hi there!

Thanks for this, this is awesome, I’ve been looking for a better way of creating a list view and this did the trick by pointing me in the right direction. How would you add a different image to each list item using your method? I’d appreciate any pointers you might have.

Thank you! Pointed me in the right direction to get it sorted. Any chance I could request a tutorial? I’d like to load local HTML files from the device depending on the list item checked and I’m having a hard time. I’d appreciate any help. Thanks again.

http://javatechig.com/ javatechig

You need to use onItemClickListener for handling the list item click events as in my examplelv1.setOnItemClickListener(new OnItemClickListener() {

@JavaTechIG I used your tutorial to help me code a custom listview but I’m having trouble with the array bit and accessing the getDate() etc

Christopher Mayhew

I’ve adjusted this to work with my code but the following bit is throwing errors. holder.headlineView.setText(listData.get(position).getHeadline()); holder.reporterNameView.setText(“By, ” + listData.get(position).getReporterName()); holder.reportedDateView.setText(listData.get(position).getDate());

I get this error on them lines The method getHeadline() is undefined for the type Object

Any help?!

http://javatechig.com/ javatechig

Hi Christopher, Instead of directly accessing methods, you may typecast it to its type. Use the below code