{"id":600,"date":"2016-01-15T07:47:39","date_gmt":"2016-01-15T07:47:39","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=600"},"modified":"2019-06-12T13:23:37","modified_gmt":"2019-06-12T13:23:37","slug":"arrayadapter-tutorial-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/arrayadapter-tutorial-example.html","title":{"rendered":"ArrayAdapter Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In android, An adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on different views like listview, gridview, spinner etc. ArrayAdapter is more simple and commonly used Adapter in android.<\/p>\n<ul>\n<li>Whenever you have a list of single type of items which is backed by an array, you can use ArrayAdapter. For instance, list of phone contacts, countries or names.<\/li>\n<li>By default, ArrayAdapter expects a Layout with a single TextView, If you want to use more complex views means more customization in grid items or list items, please avoid ArrayAdapter and use custom adapters.<\/li>\n<\/ul>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> ArrayAdapter is an implementation of BaseAdapter so if we need to create a\u00a0custom list view or \u00a0a grid view \u00a0then we have to\u00a0create our own\u00a0custom adapter and extend ArrayAdapter in that custom class. By doing this\u00a0we can override \u00a0all the function\u2019s of BaseAdapter in our custom adapter.<\/p>\n<p><span style=\"color: #008000;\"><strong>Here is code of ArrayAdapter in Android:<\/strong><\/span><\/p>\n<pre>ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)\r\n<\/pre>\n<p>In the above code snippet is the implementation of a ArrayAdapter. Below is the description of all the parameters used for the implementation of a ArrayAdapter to show a list of elements in a list view or in a spinner.<\/p>\n<p><span style=\"text-decoration: underline;\"><span style=\"color: #008000;\"><strong>Parameters used in ArrayAdapter:<\/strong><\/span><\/span><\/p>\n<p>Lets discuss parameter in ArrayAdapter class:<\/p>\n<ul>\n<li><span style=\"color: #008000;\"><strong>context:<\/strong><\/span><\/li>\n<\/ul>\n<p>The first parameter is used to pass the context means the reference of current class. Here this is a keyword used to show the current class reference. We can also use getApplicationContext(), getActivity() in the place of this keyword. getApplicationContext() is used in a Activity and getActivity() is used in\u00a0 a Fragment.<\/p>\n<p>Below is the example code in which we set the current class reference in a adapter.<\/p>\n<pre>ArrayAdapter arrayAdapter = new ArrayAdapter(this, int resource, int textViewResourceId, T[] objects);\r\n<\/pre>\n<ul>\n<li><span style=\"color: #008000;\"><strong>resource:<\/strong><\/span><\/li>\n<\/ul>\n<p>The second parameter is resource id used to set the layout(xml file) for list items in which you have a text view.<\/p>\n<p>Below is the example code in which we set the layout.<\/p>\n<pre>ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, int textViewResourceId, T[] objects);\r\n<\/pre>\n<ul>\n<li><span style=\"color: #008000;\"><strong>textViewResourceId:<\/strong><\/span><\/li>\n<\/ul>\n<p>The third parameter is textViewResourceId which is used to set the id of TextView where you want to display the actual text.<\/p>\n<p>Below is the example code in which we set the id(identity) of a text view.<\/p>\n<pre>ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, R.id.textView, T[] objects);\r\n<\/pre>\n<ul>\n<li><span style=\"color: #008000;\"><strong>objects:<\/strong><\/span><\/li>\n<\/ul>\n<p>The fourth parameter is an array of objects, used to set the array of elements in the textView. We can set the object of array or array list here.<\/p>\n<p>Below is the example code in which we set the Animal array in adapter to display the Animal name\u2019s list.<\/p>\n<pre>String animalList[] = {\"Lion\",\"Tiger\",\"Monkey\",\"Elephant\",\"Dog\",\"Cat\",\"Camel\"};\r\nArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, R.id.textView, animalList);\r\n<\/pre>\n<hr \/>\n<h4><strong>Example of an ArrayAdapter:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> Below is the example, in which we displays a list of animal names in a list view using simple array adapter. Below is the final output and code:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ArrayAdapterExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-607\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Listview.jpg\" alt=\"ArrayAdapter Example in Android Using Listview\" width=\"350\" height=\"619\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Listview.jpg 350w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Listview-170x300.jpg 170w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ArrayAdapterExample.<\/p>\n<pre>Open Android Studio -&gt; Select File -&gt; New -&gt; New Project. Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now open app -&gt; res -&gt; layout -&gt;\u00a0<strong>xml (or) activity_main.xml<\/strong>\u00a0and add following code :<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;ListView\r\n        android:id=\"@+id\/simpleListView\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:divider=\"#000\"\r\n        android:dividerHeight=\"2dp\"\/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Create a new Activity activity_list_view.xml and add the below code<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\"&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:padding=\"@dimen\/activity_horizontal_margin\"\r\n        android:textColor=\"#000\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Now Open app -&gt; java -&gt; package -&gt;\u00a0MainActivity.java and add the below code. Here we will use ArrayAdapter to display the items in Listview.<\/p>\n<pre>package example.abhiandriod.arrayadapterexample; \/\/use package of your project\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ListView;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n    \/\/ Array of strings...\r\n    ListView simpleList;\r\n    String animalList[] = {\"Lion\",\"Tiger\",\"Monkey\",\"Elephant\",\"Dog\",\"Cat\",\"Camel\"};\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        simpleList = (ListView) findViewById(R.id.simpleListView);\r\n\r\n        ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.activity_list_view, R.id.textView, animalList);\r\n        simpleList.setAdapter(arrayAdapter);\r\n    }\r\n       \r\n    }\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the App in Emulator and you will see the below output:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-607\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Listview-170x300.jpg\" alt=\"ArrayAdapter Example in Android Using Listview\" width=\"170\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Listview-170x300.jpg 170w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Listview.jpg 350w\" sizes=\"auto, (max-width: 170px) 100vw, 170px\" \/><\/center><\/p>\n<hr \/>\n<p><span style=\"color: #008000;\"><strong>Example 2: <\/strong><\/span>Below is the example, In which we display a list of Animal\u2019s name in spinner using ArrayAadapter. Below is the final output and code:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ArrayAdapterExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-608\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Spinner.jpg\" alt=\"ArrayAdapter Example in Android Using Spinner\" width=\"344\" height=\"619\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Spinner.jpg 344w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Spinner-167x300.jpg 167w\" sizes=\"auto, (max-width: 344px) 100vw, 344px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ArrayAdapterExample.<\/p>\n<pre>Select File -&gt; New -&gt; New Project. Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Now open app -&gt; res -&gt; layout -&gt; activity_main.xml (or) main.xml\u00a0and add following code:<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;Spinner\r\n        android:id=\"@+id\/animalNamesSpinner\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerInParent=\"true\"\/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Now create a new Activity simple_spinner_item.xml and add the below code:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;TextView xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\" \r\n    android:id=\"@android:id\/text1\"\r\n    style=\"?android:attr\/spinnerItemStyle\"\r\n    android:singleLine=\"true\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:ellipsize=\"marquee\"\r\n    android:textAlignment=\"inherit\"\/&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Now open app -&gt; java -&gt; package -&gt;\u00a0MainActivity.java and add the following code<\/p>\n<pre>package example.abhiandriod.arrayadapterexample;\r\n\r\nimport android.app.Activity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.AdapterView;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.Spinner;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends Activity implements\r\n        AdapterView.OnItemSelectedListener {\r\n    \/\/ Array of strings...\r\n    String animalList[] = {\"Lion\", \"Tiger\", \"Monkey\", \"Elephant\", \"Dog\", \"Cat\", \"Camel\"};\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/Getting the instance of Spinner and applying OnItemSelectedListener on it\r\n        Spinner spin = (Spinner) findViewById(R.id.animalNamesSpinner);\r\n        spin.setOnItemSelectedListener(this);\r\n\r\n        \/\/Creating the ArrayAdapter instance having the animal name's list\r\n        ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, animalList);\r\n        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);\r\n        \/\/Setting the ArrayAdapter data on the Spinner\r\n        spin.setAdapter(aa);\r\n    }\r\n\r\n\r\n    \/\/Performing action onItemSelected and onNothing selected\r\n    @Override\r\n    public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int position, long id) {\r\n        Toast.makeText(getApplicationContext(), animalList[position], Toast.LENGTH_LONG).show();\r\n    }\r\n\r\n    @Override\r\n    public void onNothingSelected(AdapterView&lt;?&gt; arg0) {\r\n        \/\/ TODO Auto-generated method stub\r\n\r\n    }\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the App in Emulator \/ AVD and you will see the animals name listed in a Spinner<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-608\" src=\"\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Spinner-167x300.jpg\" alt=\"ArrayAdapter Example in Android Using Spinner\" width=\"167\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Spinner-167x300.jpg 167w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/01\/ArrayAdapter-Example-in-Android-Using-Spinner.jpg 344w\" sizes=\"auto, (max-width: 167px) 100vw, 167px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In android, An adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on different views like listview, gridview, spinner etc. &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/arrayadapter-tutorial-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ArrayAdapter Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":607,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[62,1],"tags":[],"class_list":["post-600","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adapter","category-archieve"],"acf":[],"psp_head":"<title>ArrayAdapter Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on ArrayAdapter with examples in Android Studio which list single type of items backed by an array. Also find explanation about parameter used in ArrayAdapter.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/arrayadapter-tutorial-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=600"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":2840,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/600\/revisions\/2840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/607"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}