Create a custom List using Simple Adapter

In android, An adapter is a bridge between UI component and data source that helps us to fill data in UI component SimpleAdapter is used for customization of list or grid items.

In Android, SimpleAdapter is an easy adapter to map static data to views defined in an XML file (layout). In android you can specify the data backing to a list as an ArrayList of Maps(hashmap or other). Each entry in a ArrayList is corresponding to one row of a list.

SimpleAdapter code:

Parameters used in SimpleAdapter:

1. context:

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.

2. data:

The second parameter is data which is a List of Maps. Each entry in a List corresponds to one row in the list.

3. resource:

The third parameter is resource id which is use to set the layout (xml file) for list items in which you have a text view, image view or any other view.

Suppose below is the code of Activity activity_design.xml where we define ImageView and TextView as list items which will be set in Layout:

Now we will pass this layout as 3rd parameter in SimpleAdapter.

4. from:

The fourth parameter is from is an string array or called a list of column names that will be added to a Map associated with each item of a grid or list view.

5. to:

The fifth and last parameter is to which is an integer array used to store the Id’s of the views.

Example of Simple Adapter :

Step 1: Create a new project in Android Studio and name it SimpleAdapterExample.

Step 2: Now go to res -> layout -> xml (or) activity_main.xml and add following code. Here we are designing ListView in Relative Layout.

Step 3: Now Create a new Activity. Go to res-> right click on layout-> new -> Activity -> Blank Activity and create activity_design.xml activity. Here add following code. Here we are defining ImageView and TextView items that will be displayed inside each row of ListView.

Step 4: Now open app -> java -> package, click on MainActivity.java and add the below code. Here we will use SimpleAdapter to display static data in XML layout. More explanation is included in the code itself.

Output  :-

 

About Jaimin Patel

I am a Web Developer. I have been blogging and researching since 1 years and like to share my knowledge and research with the world. Peace..!!

View all posts by Jaimin Patel →

Leave a Reply

Your email address will not be published. Required fields are marked *