ArrayAdapter sample program in Android

By: Ashley  

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>



Archived Comments

1. We're a bunch of volunteers and opening a brand new scheme in our community.
Your site offere

View Tutorial          By: Google.com at 2017-09-08 16:05:43

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

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

4. android.R.layout.simple_expandable_list_item_1,
items

where did the ge

View Tutorial          By: ananth at 2016-03-01 00:19:32

5. android.R.layout.simple_expandable_list_item_1,
items

where did the ge

View Tutorial          By: ananth at 2016-03-01 00:13:29

6. Thanks

I was really helpful.

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

7. Very useful and simple.
Thanks.

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

8. setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simp

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

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

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

11. Thanks man, really very helpful..,.
Thanks again..,.

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

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

13. Thanks a ton. Was struggling to get the ListView up and running. I didn't knew what is the second pa
View Tutorial          By: Avinash at 2012-09-25 12:25:12

14. Thank you!
Good example!

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

15. Thanks!
Please point me to a site which discusses options on your very clear example. For e

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


Most Viewed Articles (in Android )

Latest Articles (in Android)

Comment on this tutorial