ArrayAdapter sample program in Android

By: Ashley Printer Friendly Format    

This sample android program shows you how to use ArrayAdapter in Android. In this program a list is shown. When you click on the list, the selected item is shown on the text view. You can use this ArrayAdapter widget and the onListItemClick() method to determine the selected index and process accordingly.

The ArrayAdapterDemo.java file is as follows:

package com.javasamples;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ArrayAdapterDemo extends ListActivity {
   TextView selection;
   String[] items = { "this", "is", "a", "really", 
   "silly", "list" };
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
      this,
      android.R.layout.simple_expandable_list_item_1,
      items));
selection=(TextView)findViewById(R.id.selection); 
   }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String text = " position:" + position + "  " + items[position];
selection.setText(text);
}
      
}

The output of this program will be as shown in the android emulator below.

The main.xml file in your res/layout folder is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:background="#ff0000cc" 
        android:textStyle="bold"/>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:choiceMode="multipleChoice"
        />
</LinearLayout>


Ask a Question



Most Viewed Articles (in Android )

Latest Articles (in Android)

Comment on this tutorial

Ask a Question

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Thanks!
Please point me to a site which di

View Tutorial          By: TERRY DEGLOW at 2012-01-29 16:04:41

2. Thank you!
Good example!

View Tutorial          By: Torbj√∂rn Molvig at 2012-09-18 08:25:34

3. Thanks a ton. Was struggling to get the ListView u
View Tutorial          By: Avinash at 2012-09-25 12:25:12

4. Thanks Man!! Been searching for a simple one like
View Tutorial          By: Kid at 2012-10-12 14:15:43

5. Thanks man, really very helpful..,.
Thanks

View Tutorial          By: Manish at 2012-10-18 06:19:03

6. thanks a lot man....
View Tutorial          By: Praveen at 2012-12-03 08:36:51

7. thanks man really very nice and helpful...........
View Tutorial          By: Bakhtawar khan at 2012-12-05 05:54:18

8. setListAdapter(new ArrayAdapter<String>(

View Tutorial          By: Mark at 2013-02-02 02:25:42

9. Very useful and simple.
Thanks.

View Tutorial          By: harsh at 2014-11-06 10:01:22

10. Thanks

I was really helpful.

View Tutorial          By: saroj khatiwoda at 2015-06-02 12:17:51

11. android.R.layout.simple_expandable_list_item_1,
View Tutorial          By: ananth at 2016-03-01 00:13:29

12. android.R.layout.simple_expandable_list_item_1,
View Tutorial          By: ananth at 2016-03-01 00:19:32

13. Andrey
View Tutorial          By: Andrey at 2017-05-25 21:54:39

14. geomineral.ru
View Tutorial          By: geomineral.ru at 2017-06-09 18:23:22

15. We're a bunch of volunteers and opening a brand ne
View Tutorial          By: Google.com at 2017-09-08 16:05:43