Android ArrayAdapter

ArrayAdapter is a useful generic class in Android. It can be used to supply data items to a Spinner or ListView widget. To create an instance of ArrayAdapter, you can use its constructor as shown below.


ArrayAdapter<T>(Context context, int resource,T[] objects)


The context argument is the current context. It can be the current activity. For the resource argument you need the name of a layout file in which a TextView is placed. The ArrayAdapter uses TextViews to display items of a Spinner or ListView. To use something other than TextViews to display the items, you have to override getView(int, View, ViewGroup) to return the type of view you want. The Android Spinner customized to show text and images tutorial might be useful to you in this case. The objects argument is an array or ArrayList that stores the items of the Spinner or ListView.
After you created an instance of the ArrayAdapter, you can set it to the Spinner or ListView by calling the setAdapter(ArrayAdapter adapter) method.



Now to have an example application on using the ArrayAdapter with Spinner widget, create a new Android project. Then modify the activity_main.xml file to add a Spinner as shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"

   >
  
   <Spinner
      android:id="@+id/spin"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
         />
</RelativeLayout>


You will create another layout file called spinner_item.xml file that contains a TextView.

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/text1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
  
   />
In the onCreate() method of the MainActivity class, create a reference to the Spinner, an array of items, an instance of the ArrayAdapter and set the instance to the Spinner by using the setAdapter() method.

  package com.example.androidexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner spinner;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//reference to the current activity
context=this;
//reference to the Spinner
spinner=(Spinner)findViewById(R.id.spin);
//array containing items of the Spinner
   String[] currencyList={"AUD","CAD","CHF","EUR","GBP","JPY","NZD","KHR","USD","CNY","THB","INR"};
   //wrap the items in the Adapter
   ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.spinner_item,currencyList);
   //assign adapter to the Spinner
   spinner.setAdapter(adapter);
  

}


}


Android-ArrayAdapter

Posted by: Dara | post date: 09-14-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.