{"id":999,"date":"2016-04-23T06:46:46","date_gmt":"2016-04-23T06:46:46","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=999"},"modified":"2019-06-12T06:04:54","modified_gmt":"2019-06-12T06:04:54","slug":"adapter","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/adapter","title":{"rendered":"Adapter Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, 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 an Adapter view then view can takes the data from the adapter view and shows the data on different views like as ListView, GridView, Spinner etc. For more customization in Views we uses the base adapter or custom adapters.<\/p>\n<p>To fill data in a list or a grid we need to implement Adapter. Adapters acts\u00a0like a bridge between UI component and data source. Here data source is the source from where we get the data and UI components are list or grid items in which we want to display that data.<\/p>\n<hr \/>\n<p><strong>Below is a conceptual diagram of Adapter:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1613\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/Adapter-in-Android.jpg\" alt=\"Adapter in Android\" width=\"615\" height=\"478\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/Adapter-in-Android.jpg 615w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/Adapter-in-Android-300x233.jpg 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<hr \/>\n<h4><strong>Adapters In Android:<\/strong><\/h4>\n<p>There are the some commonly used Adapter in Android used to fill the data in the UI components.<\/p>\n<ol>\n<li>BaseAdapter &#8211;\u00a0It is parent adapter for all other adapters<\/li>\n<li>ArrayAdapter &#8211; It is\u00a0used whenever we\u00a0have a list of single items which is backed by an array<\/li>\n<li>Custom ArrayAdapter &#8211;\u00a0It\u00a0is used whenever we\u00a0need to display a custom list<\/li>\n<li>SimpleAdapter &#8211;\u00a0It\u00a0is an easy adapter to map static data to views defined in your XML file<\/li>\n<li>Custom SimpleAdapter &#8211;\u00a0It\u00a0is used whenever we\u00a0need to display a customized list and needed to access the child items of the list or grid<\/li>\n<\/ol>\n<p><strong>Now we\u00a0describe each Adapters one by one in detail:<\/strong><\/p>\n<p><span style=\"color: #008000;\"><strong>1. BaseAdapter In Android:<\/strong><\/span><\/p>\n<p>BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView, GridView, Spinner etc. Whenever we\u00a0need a customized list in a ListView or customized grids in a GridView we\u00a0create our own adapter and extend base adapter in that. Base Adapter can be extended to create a custom Adapter for displaying a custom list item. \u00a0ArrayAdapter is also an implementation of BaseAdapter.<\/p>\n<p><strong>Custom Adapter\u00a0code which\u00a0extends the BaseAdapter in that:<\/strong><\/p>\n<pre>public class CustomAdapter extends BaseAdapter {\r\n\r\n@Override\r\npublic int getCount() {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic Object getItem(int i) {\r\nreturn null;\r\n}\r\n\r\n@Override\r\npublic long getItemId(int i) {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic View getView(int i, View view, ViewGroup viewGroup) {\r\n\r\nreturn null;\r\n}<\/pre>\n<p>In above code snippet we see the overrided functions of BaseAdapter which are used to set the data in a list, grid or a spinner. These functions are described in <a href=\"\/ui\/baseadapter-tutorial-example.html\">BaseAdapter tutorial with example<\/a>.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. ArrayAdapter In Android:<\/strong><\/span><\/p>\n<p>Whenever we\u00a0have a list of single items which is backed by an Array, we\u00a0can use ArrayAdapter. For instance, list of phone contacts, countries or names.<\/p>\n<p><strong>Here is how android ArrayAdapter looks ::<\/strong><\/p>\n<pre>ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)\r\n<\/pre>\n<p>The above function are described in <a href=\"\/ui\/arrayadapter-tutorial-example.html\">ArrayAdapter tutorial with example<\/a>.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. Custom ArrayAdapter In Android:<\/strong><\/span><\/p>\n<p>ArrayAdapter is also an implementation of BaseAdapter, so if we want more customization then we can create a custom adapter and extend ArrayAdapter in that. Since array adapter is an implementation of BaseAdapter, so we can override all the function\u2019s of BaseAdapter in our custom adapter.<\/p>\n<p><strong>Below Custom adapter class MyAdapter extends ArrayAdapter in that:<\/strong><\/p>\n<pre>public class MyAdapter extends ArrayAdapter {\r\n\r\npublic MyAdapter(Context context, int resource, int textViewResourceId, List objects) {\r\nsuper(context, resource, textViewResourceId, objects);\r\n}\r\n@Override\r\npublic int getCount() {\r\nreturn super.getCount();\r\n}\r\n\r\n@Override\r\npublic View getView(int position, View convertView, ViewGroup parent) {\r\nreturn super.getView(position, convertView, parent);\r\n}\r\n}\r\n<\/pre>\n<p>These functions are described in <a href=\"\/ui\/custom-arrayadapter-tutorial-example.html\">Custom ArrayAdapter tutorial with example<\/a>.<\/p>\n<p><span style=\"color: #008000;\"><strong>4. SimpleAdapter In Android:<\/strong><\/span><\/p>\n<p>In Android\u00a0 SimpleAdapter is an easy Adapter to map static data to views defined in an XML file(layout). In Android we\u00a0can specify the data backing to a list as an ArrayList of <a href=\"\/java\/map\" target=\"_blank\">Maps<\/a>(i.e. hashmap or other). Each entry in a ArrayList is corresponding to one row of a list.<\/p>\n<p>The Map contains the data for each row. Here we\u00a0also specify an XML file(custom list items file) that defines the views which is used to display the row, and a mapping from keys in the Map to specific views.<\/p>\n<p>Whenever we have to create a custom list we need to implement custom adapter. As we discuss earlier ArrayAdapter is used when we have a list of single item\u2019s backed by an Array. So if we need more customization in a ListView or a GridView we need to implement simple adapter.<\/p>\n<p><strong>SimpleAdapter code in Android:<\/strong><\/p>\n<pre>SimpleAdapter\u00a0(Context\u00a0context,\u00a0List&lt;?\u00a0extends\u00a0Map&lt;String,\u00a0?&gt;&gt; data, int resource,\u00a0String[]\u00a0from, int[] to)\r\n<\/pre>\n<p>The above parameters of a simple Adapter is described in <a href=\"\/ui\/simpleadapter.html\">SimpleAdapter tutorial with example<\/a>.<\/p>\n<p><span style=\"color: #008000;\"><strong>5. Custom SimpleAdapter In Android:<\/strong><\/span><\/p>\n<p>Whenever we have to create a custom list we need to implement custom adapter. As we discuss earlier ArrayAdapter is used when we have a list of single item\u2019s backed by an Array. So if we need customization in a ListView or a GridView we need to implement simple Adapter but when we need more customization in list or grid items where we have many view\u2019s in a list item and then we have to perform any event like click or any other event to a particular view then we need to implement a custom adapter who fulfills our requirement\u2019s and quite easy to be implemented.<\/p>\n<p>BaseAdapter is the parent adapter for all other adapters so if we extends a SimpleAdapter then we can also override the base adapter\u2019s function in that class.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Important\u00a0Note:<\/span>\u00a0<\/strong>We can\u2019t perform events like click and other event on child item of a list or grid but if we have some requirements to do that then we can create our own custom adapter and extends the simple adapter in that.<\/p>\n<p><strong>Custom Adapter extends SimpleAdapter in that:<\/strong><\/p>\n<pre>public class CustomAdapter extends SimpleAdapter {\r\npublic CustomAdapter(Context context, List&lt;? extends Map&lt;String, ?&gt;&gt; data, int resource, String[] from, int[] to) {\r\nsuper(context, data, resource, from, to);\r\n\r\n}\r\n\r\n@Override\r\npublic View getView(int position, View convertView, ViewGroup parent) {\r\nreturn super.getView(position, convertView, parent);\r\n\r\n}\r\n\r\n@Override\r\npublic int getCount() {\r\nreturn super.getCount();\r\n}\r\n}\r\n<\/pre>\n<p>The above overrided functions of simple adapter are already described in <a href=\"\/ui\/custom-simpleadapter.html\">Custom SimpleAdapter<\/a> article.<\/p>\n<hr \/>\n<h4><strong>Adapter Example In Android Studio:<\/strong><\/h4>\n<p>Below is the Android Studio example which show the use of the Adapter in Android. In this example we display a list of\u00a0fruits names with images by using SimpleAdapter and whenever user\u00a0click on a list item the fruit\u2019s name displayed in a Toast.<\/p>\n<p>Below you can download complete Android Studio code, see final output and read step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/SimpleAdapterExample\" 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><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1615\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/Adapter-example-in-Android-Studio.jpg\" alt=\"Adapter example in Android Studio\" width=\"274\" height=\"429\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/Adapter-example-in-Android-Studio.jpg 274w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/Adapter-example-in-Android-Studio-192x300.jpg 192w\" sizes=\"auto, (max-width: 274px) 100vw, 274px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it SimpleAdapterExample.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt;\u00a0xml (or) main.xml\u00a0and add following code :<\/p>\n<p>In this step we open an xml file and add the code for displaying a ListView by using its different attributes.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nxmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\ntools:context=\".MainActivity\"&gt;\r\n\r\n&lt;ListView\r\nandroid:id=\"@+id\/simpleListView\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:divider=\"#000\"\r\nandroid:dividerHeight=\"2dp\"\r\nandroid:listSelector=\"#600\"\/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Save fruit images in drawable folder with\u00a0name apple, banana, litchi, mango and pineapple.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Open src -&gt; package -&gt;\u00a0MainActivity.java<\/p>\n<p>In this step we add the code for initiate ListView and set the data in the list. In this firstly we create two\u00a0arrays first\u00a0for\u00a0fruit names and second for fruits images and then set the data in the ListView using SimpleAdapter.<\/p>\n<pre>package example.abhiandriod.simpleadapterexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.AdapterView;\r\nimport android.widget.ListView;\r\nimport android.widget.SimpleAdapter;\r\nimport android.widget.Toast;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.HashMap;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    \/\/initialize view's\r\n    ListView simpleListView;\r\n    String[] fruitsNames = {\"Apple\", \"Banana\", \"Litchi\", \"Mango\", \"PineApple\"};\/\/fruit names array\r\n    int[] fruitsImages = {R.drawable.apple, R.drawable.banana, R.drawable.litchi, R.drawable.mango, R.drawable.pineapple};\/\/fruits images\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        simpleListView=(ListView)findViewById(R.id.simpleListView);\r\n\r\n        ArrayList&lt;HashMap&lt;String,String&gt;&gt; arrayList=new ArrayList&lt;&gt;();\r\n        for (int i=0;i&lt;fruitsNames.length;i++)\r\n        {\r\n            HashMap&lt;String,String&gt; hashMap=new HashMap&lt;&gt;();\/\/create a hashmap to store the data in key value pair\r\n            hashMap.put(\"name\",fruitsNames[i]);\r\n            hashMap.put(\"image\",fruitsImages[i]+\"\");\r\n            arrayList.add(hashMap);\/\/add the hashmap into arrayList\r\n        }\r\n        String[] from={\"name\",\"image\"};\/\/string array\r\n        int[] to={R.id.textView,R.id.imageView};\/\/int array of views id's\r\n        SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.list_view_items,from,to);\/\/Create object and set the parameters for simpleAdapter\r\n        simpleListView.setAdapter(simpleAdapter);\/\/sets the adapter for listView\r\n\r\n        \/\/perform listView item click event\r\n        simpleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {\r\n            @Override\r\n            public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {\r\n                Toast.makeText(getApplicationContext(),fruitsNames[i],Toast.LENGTH_LONG).show();\/\/show the selected image in toast according to position\r\n            }\r\n        });\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 5:<\/strong><\/span> Create new layout-<strong>&gt; <\/strong>rec-&gt; layout-&gt; list_view_items.xml and add following code:<\/p>\n<p>In this step we create a xml file for displaying ListView items. In this xml we add the code for displaying a ImageView and a TextView.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#fff\"&gt;\r\n\r\n&lt;ImageView\r\nandroid:id=\"@+id\/imageView\"\r\nandroid:layout_width=\"50dp\"\r\nandroid:layout_height=\"50dp\"\r\nandroid:padding=\"5dp\"\r\nandroid:layout_alignParentRight=\"true\"\r\nandroid:layout_marginRight=\"10dp\"\r\nandroid:src=\"@drawable\/ic_launcher\" \/&gt;\r\n\r\n&lt;TextView\r\nandroid:id=\"@+id\/textView\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:padding=\"@dimen\/activity_horizontal_margin\"\r\nandroid:text=\"Demo\"\r\nandroid:textColor=\"#000\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><strong>\u00a0Output:<\/strong><\/p>\n<p>Now run the App and you will different fruit names listed in ListView. Here we used Simple Adapter to fill data in ListView.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, 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 an Adapter view then view can takes the data from the adapter view and shows the data on different views like as ListView, GridView, Spinner &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/adapter\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Adapter Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-999","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Adapter Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Complete Adapter tutorial with step by step explanation of example in Android Studio. Also learn about Base Adapter, Array Adapter, Simple Adapter, Custom Simple Adapter and Custom Array Adapter in Android.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/adapter\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"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=999"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/999\/revisions"}],"predecessor-version":[{"id":2770,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/999\/revisions\/2770"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}