Adapter Tutorial With Example In Android Studio | Abhi Android

Adapter Tutorial With Example In Android Studio

In Android, Adapter is a bridge between UI component and data generator that helps us to fill data in UI component. It holds the datum and send the datum to an Adapter position then view can takes the data from the arranger opinion and shows the data on different views like as ListView, GridView, Spinner etc. For more customization in Views we uses the base arranger or custom adapters .
To fill data in a list or a grid we need to implement Adapter. Adapters acts like a bridge between UI part 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 datum .
Below is a conceptual diagram of Adapter:

Adapter in AndroidAdapter in Android

Adapters In Android:

There are the some normally used Adapter in Android used to fill the data in the UI components .

  1. BaseAdapter – It is parent adapter for all other adapters
  2. ArrayAdapter – It is used whenever we have a list of single items which is backed by an array
  3. Custom ArrayAdapter – It is used whenever we need to display a custom list
  4. SimpleAdapter – It is an easy adapter to map static data to views defined in your XML file
  5. Custom SimpleAdapter – It is used whenever we need to display a customized list and needed to access the child items of the list or grid

Now we describe each Adapters one by one in detail:
1. BaseAdapter In Android:
BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView, GridView, Spinner etc. Whenever we need a customize list in a ListView or customized grids in a GridView we create our own adapter and extend base arranger in that. Base Adapter can be extended to create a custom Adapter for displaying a custom-made list detail. ArrayAdapter is besides an implementation of BaseAdapter .
Custom Adapter code which extends the BaseAdapter in that:

public class CustomAdapter extends BaseAdapter {

@Override
public int getCount() {
return 0;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

return null;
}

In above code snip we see the overrule functions of BaseAdapter which are used to set the data in a tilt, grid or a spinner. These functions are described in BaseAdapter tutorial with model .
2. ArrayAdapter In Android:
Whenever we have a list of single items which is backed by an Array, we can use ArrayAdapter. For case, list of telephone contacts, countries or names .
Here is how android ArrayAdapter looks ::

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

The above affair are described in ArrayAdapter tutorial with exercise .
3. Custom ArrayAdapter In Android:
ArrayAdapter is besides an implementation of BaseAdapter, so if we want more customization then we can create a custom-made adapter and extend ArrayAdapter in that. Since range arranger is an implementation of BaseAdapter, so we can override all the function ’ second of BaseAdapter in our custom arranger .
Below Custom adapter class MyAdapter extends ArrayAdapter in that:

public class MyAdapter extends ArrayAdapter {

public MyAdapter(Context context, int resource, int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public int getCount() {
return super.getCount();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
}

These functions are described in Custom ArrayAdapter tutorial with model .
4. SimpleAdapter In Android:
In Android SimpleAdapter is an comfortable Adapter to map static data to views defined in an XML file ( layout ). In Android we can specify the data back to a list as an ArrayList of Maps ( i.e. hashmap or other ). Each entry in a ArrayList is corresponding to one row of a number .
The Map contains the datum for each rowing. here we besides specify an XML file ( custom tilt items file ) that defines the views which is used to display the quarrel, and a map from keys in the Map to specific views .
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 ’ sulfur backed by an Array. thus if we need more customization in a ListView or a GridView we need to implement elementary adapter .
SimpleAdapter code in Android:

SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)

The above parameters of a elementary Adapter is described in SimpleAdapter tutorial with exemplar .
5. Custom SimpleAdapter In Android:
Whenever we have to create a custom-made list we need to implement custom-made arranger. As we discuss earlier ArrayAdapter is used when we have a tilt of individual item ’ randomness backed by an Array. therefore if we need customization in a ListView or a GridView we need to implement simpleton Adapter but when we need more customization in list or grid items where we have many view ’ sulfur in a list item and then we have to perform any event like chatter or any other consequence to a particular view then we need to implement a custom arranger who fulfills our prerequisite ’ mho and quite slowly to be implemented .
BaseAdapter is the rear arranger for all other adapters therefore if we extends a SimpleAdapter then we can besides override the base arranger ’ s function in that class .
Important Note:  We can ’ thyroxine perform events like click and early consequence on child detail of a list or grid but if we have some requirements to do that then we can create our own custom arranger and extends the simple adapter in that .
Custom Adapter extends SimpleAdapter in that:

public class CustomAdapter extends SimpleAdapter {
public CustomAdapter(Context context, List> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);

}

@Override
public int getCount() {
return super.getCount();
}
}

The above overrided functions of simple adapter are already described in Custom SimpleAdapter article.

Adapter Example In Android Studio:

Below is the Android Studio case which show the use of the Adapter in Android. In this example we display a list of fruits names with images by using SimpleAdapter and whenever drug user snap on a list token the fruit ’ south name displayed in a Toast .
Below you can download complete Android Studio code, see final output and read footprint by step explanation :
Download Code ?
Adapter example in Android StudioAdapter example in Android Studio
Step 1: Create a new visualize and name it SimpleAdapterExample .
Step 2: Open res – > layout – > xml ( or ) main.xml and add following code :
In this mistreat we open an xml file and add the code for displaying a ListView by using its different attributes .





Step 3: Save yield images in drawable booklet with name apple, banana, litchi, mango and pineapple .
Step 4: Open src – > box – > MainActivity. java
In this gradation we add the code for initiate ListView and set the data in the tilt. In this first we create two arrays first base for fruit names and second for fruits images and then set the data in the ListView using SimpleAdapter .

package example.abhiandriod.simpleadapterexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    //initialize view's
    ListView simpleListView;
    String[] fruitsNames = {"Apple", "Banana", "Litchi", "Mango", "PineApple"};//fruit names array
    int[] fruitsImages = {R.drawable.apple, R.drawable.banana, R.drawable.litchi, R.drawable.mango, R.drawable.pineapple};//fruits images
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleListView=(ListView)findViewById(R.id.simpleListView);

        ArrayList> arrayList=new ArrayList<>();
        for (int i=0;i hashMap=new HashMap<>();//create a hashmap to store the data in key value pair
            hashMap.put("name",fruitsNames[i]);
            hashMap.put("image",fruitsImages[i]+"");
            arrayList.add(hashMap);//add the hashmap into arrayList
        }
        String[] from={"name","image"};//string array
        int[] to={R.id.textView,R.id.imageView};//int array of views id's
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.list_view_items,from,to);//Create object and set the parameters for simpleAdapter
        simpleListView.setAdapter(simpleAdapter);//sets the adapter for listView

        //perform listView item click event
        simpleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),fruitsNames[i],Toast.LENGTH_LONG).show();//show the selected image in toast according to position
            }
        });
    }

    
}

Step 5: Create new layout- > rec- > layout- > list_view_items.xml and add following code :
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 .








 Output:
now run the App and you will different fruit names listed in ListView. here we used Simple Adapter to fill data in ListView .

source : https://thefartiste.com
Category : Tech

About admin

I am the owner of the website thefartiste.com, my purpose is to bring all the most useful information to users.

Check Also

articlewriting1

Manage participants in a zoom meeting webinar

Call the people who attend the meet as follows Alternate host host Who scheduled the …

Leave a Reply

Your email address will not be published.