ArrayAdapter sample program in Android

By: Ashley Emailed: 1676 times Printed: 2155 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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>

Android Home | All Android Tutorials | Latest Android Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(9)


1. View Comment

Thanks!
Please point me to a site which discusses options on your very clear example. For example, using another xml file for the strings, or a built in listview file instead of one expressly coded as you did. Your example is great since is so clear.


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

Thanks a ton. Was struggling to get the ListView up and running. I didn't knew what is the second parameter of ArrayAdapter's constructor but this sample made it clear !

View Tutorial          By: Avinash at 2012-09-25 12:25:12
3. View Comment

Thanks Man!! Been searching for a simple one like this! :D

View Tutorial          By: Kid at 2012-10-12 14:15:43
4. View Comment

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


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

thanks a lot man....

View Tutorial          By: Praveen at 2012-12-03 08:36:51
6. View Comment

thanks man really very nice and helpful......................

View Tutorial          By: Bakhtawar khan at 2012-12-05 05:54:18
7. View Comment

setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_expandable_list_item_1,
items));
this error every time i add it it reflect error and ask for creating that code
private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub

}


View Tutorial          By: Mark at 2013-02-02 02:25:42
8. View Comment

Very useful and simple.
Thanks.


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

Thanks

I was really helpful.


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

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ashley
Is autodiscover CNAME a must in Office 365
Directory Synchronization tool in Office 365
Configure Outlook Manually for Office 365
Review: Ruggear - The world's most Rugged Phone in Singapore
Solution to error: unable to open connection to server due to security error
Making your Alias email as 'Send As' in Office 365
Steps to Create Shared Mailbox in office 365
xxxx cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
The user's Outlook Live mailbox is full.
Outlook Live couldn't connect to the IMAP server.
The user's mailbox has already been migrated.
An Outlook Live mailbox wasn't found for the user.
Outlook Live couldn't sign in to the user account on the IMAP messaging system.
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial

More Tutorials in Android
Getting Started with Android
Solution to error: unable to open connection to server due to security error
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Android Emulator
Intent in Android to call one activity from another activity.
Animation sample program in Android
Another Animation sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
GridView sample program in Android
Gallery sample program in Android
Date and Time sample program in Android
Progress bar and downloading a file sample program in Android
Reading a file sample program in Android

More Latest News
Most Viewed Articles (in Android )
Progress bar and downloading a file sample program in Android
Reading and Writing a file to SD card sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
Intent in Android to call one activity from another activity.
Reading a file sample program in Android
Android Send SMS and Make a phone call in Android Application
Date and Time sample program in Android
Solution to error: unable to open connection to server due to security error
GridView sample program in Android
Gallery sample program in Android
Animation sample program in Android
Android Emulator
Android Preferences - Using Preferences in Android Tutorial
Getting Started with Android
Most Emailed Articles (in Android)
Reading and Writing a file to SD card sample program in Android
AlertDialog sample program in Android
Android Preferences - Using Preferences in Android Tutorial
ArrayAdapter sample program in Android
GridView sample program in Android
Progress bar and downloading a file sample program in Android
Getting Started with Android
Another Animation sample program in Android
Spinner sample program in Android
Gallery sample program in Android
Reading a file sample program in Android
Android Emulator
Animation sample program in Android
Error generating R.java from manifest
Date and Time sample program in Android