Android Listview Example

Today, we are going to see about a simple listview example. In Android, Listview is used to show a list of items in a vertically scrolling list.  Learn a listview of android array in this tutorial.

For instance, in a Registration form when we are selecting professions a list of items will be displayed. We can use Listview to display the list of items.

Your XML file should look like

[sourcecode language="xml"]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

[/sourcecode]

Your Java code looks like

[sourcecode language="java"]

public class ListviewExample extends Activity
{
private ListView lv1;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
}
}

[/sourcecode]

You can also customize your listview. Click here to see custom listview example

The output will look like

listviewexample

Comments

Hi fernie, look at the below

Hi fernie,

look at the below link http://developer.android.com/resources/samples/Ap...

Here you can see about how to add columns in listview. Here they have two columns, one is image & another is text.

First of all thanks for your

First of all thanks for your comments!..
Ya, you are right. We can use like that too..

Hi noelle Yes u can add

Hi noelle
Yes u can add OnClickListner or we can also use setOnItemClickListener to trigger a action on a particular item's click!
Yourlistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// your actions
}
});

Alex, The simple_list_item_1

Alex,
The simple_list_item_1 is used to design the list items.
If you use simple_list_item_checked , there will be a option to check the list items.
Same like that we can use simple_list_item_multiple_choice ,simple_list_item_single_choice,etc..
Questions ?

Hi noelle, post your doubt as

Hi noelle,
post your doubt as comments,so that it will be useful to others too.Feel free to post doubts.

Pranay, Look at the 12th

Pranay,
Look at the 12th line
lv1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1 , lv_arr));
This line helps us to set listview elements. By using this one more time we can able to change the listview elements.

For example :-
Use arraylist in ListView instead of String Array. Create a Button and set onClickListener to that Button.
In that onClickListener add elements to the arraylist and set it to ListAdapter.

Questions ?..

Parena, By using

Parena,

By using getCheckedItemPositions() you can get the selected items from listview with multiple choice.
Here is a simple example of listview multiple choice http://www.androidpeople.com/android-listview-mul...

You can see more listview example http://www.androidpeople.com/category/listview/

Hi Sindhu,

Hi Sindhu,
prbly this example should help u http://www.androidpeople.com/android-custom-listv...

Cheers :)

Hi sindhu, To

Hi sindhu,
To remove the latest intent just call finish() method. But use some thing like global variable to get the data from listview clickevent and store the data to global variable so that u can access even in the home page.

Hi madhuri ,

Hi madhuri ,
Try to have global string array and keep collecting the item as when you click on the check boxes.later u can use it any activity.

Hi mash, paste ur log cat

Hi mash,
paste ur log cat here so that we can find what causing the application to crash.

Hi Askile, If

Hi Askile,

If you use textview in listview and then by default it's multiline. The string will adjust itself. For onclick see this example http://www.androidpeople.com/android-listview-onc...

You can store large strings

You can store large strings in DB. its not an issue. if it runs out of space split into 2 paragraph and have them in two column.

Gowri, Instead of using

Gowri,
Instead of using String Array use ArrayList. By using arraylist we need not to mention size of arraylist.
After entering text in edittext click the add button.
In add button onClick() write
arrayList.add(editText.getText().toString());
lv1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1 , arrayList));

After adding the value to arrayList. Just set one more time the listview adapter.

Gowri, wat error you are

Gowri,
wat error you are facing ?...
can you please paste the logcat error here..?

:)

:)

@satya, Refer the below link

@satya,

Refer the below link http://www.androidpeople.com/android-custom-dynam...

It may help you..

@satya, At very first time

@satya,

At very first time give only

@Override

public void onCreate(Bundle icicle)

{

super.onCreate(icicle);

setContentView(R.layout.main);

lv1=(ListView)findViewById(R.id.ListView01);

}

in onclick listener

add the values to string array and then give

lv1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1 , lv_arr));

@Gayathri, Refer this link

@Gayathri,

Refer this link
http://www.androidpeople.com/android-custom-dynam...

This example will explain how to add values to listview in run time using Edittext, Button & ListView.

@satya, Refer this link for

@satya,

Refer this link for listview onclick
http://www.androidpeople.com/android-listview-onc...

@satya, Look at this example

@satya,

Look at this example

http://www.androidpeople.com/android-sliding-draw...

Sliding Drawer for Android.

@satya, Give fixed size for

@satya,

Give fixed size for listview to get 2 listviews at a time.