What is Adapter in Android



what is an Adapter?
Before learning Adapter lets watch how a general charging adapter works when
we charge our smartphone. So its a good thing before start coding to know deeper about a basic adapter.

in the above picture, you can see charging adapter makes connectivity between the board and your smartphone so basically it something which anyhow to give connectivity and conversion of electricity to dc supply and charge the phone.
Same happens with Adapter in Android it provides connectivity between data and
views somehow in the application.

How Adapter Works in Android?
there are two parts in the adapter (adapter(data), adapter(view)), adapter(data) take data from data source and its views that data in adapterView then that adapter view can bind with any view Like ListView, RecyclerView, Spinner and GridView.

there are many adapters described in Android Documentation but I am telling you about the most usable adapter to fill the data in your UI.

most use Adapter-
                  1.ArrayAdapter
                  2.Custom ArrayAdapter
                  3.BaseAdapter
                  4.SimpleAdapter
                  5. Custom SimpleAdapter
in this blog, I will explain to you about ArrayAdapter. ArrayAdapter is most commonly used as an adapter in android. when there is a single item list in an array return we use Array Adapter in there. There are many adapter layouts which are Inbuilt provided
in Android Studio.

Now I am telling you how you can create a simple List view by the help of ArrayAdapter
and inbuilt adapter layout   simple_list_item_1 .

Steps to create Simple List View with Help of ArrayAdapter-

1. Make a new Android Project.

2. Make List view in Layout by drag and drop or XML codes.

3. Take Values in form of String type Array in Android to show those data in the view.

String[] name={"abc","def","dddd","ssss"};
 
 4. Now call Array adapter and set Array on that.

 ArrayAdapter<String> ad = new 
 ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,name);
 
5. At the end set that adapter in ListView.

    listView.setAdapter(ad);

Now MainActivity.java look like below-

hope this blog will helpful for you in next few blogs I will tell how to customize listView
and how to work with RecyclerView in Detail.

                                      ProjectCode- ClickMe

Post a Comment

0 Comments