Android Custom Simple Adapter Tutorial With Example In Android Studio

Android Custom Simple Adapter Tutorial With Example In Android Studio is here.

Adapter in android is working as a link or connection between a data source and the multiple view like listview, gridview or spinner.

To connect the data source with listview in an easy manner, we can use android’s in built class named Simple Adapter.

Simple Adapter uses very less lines of codes to make listview, gridview or spinner.

Using simple adapter, we can create basic listview with limited functionality.

Custom Simple Adapter Basics

To create more complex functionalities, we need to make custom simple adapter.

Functionalities like click events of UI widgets, checkboxes maintenance, radio buttons maintenance etc. are complex events with adapters.

Custom adapter helps us to handle these events seamlessly.

Simple adapter can not inflate the view in the row layout of listview which have various UI widgets.

Here, custom simple adapter comes into the picture.

You can use custom simple adapter to develop listview, gridview or spinner with easy and simple coding lines.

Custom adapter extends the simple adapter so that we can modify or override the basic methods simple adapter.

Following is the example of the custom simple adapter

As you can see that the class is extending the Simple Adapter.

Because simple adapter is the sub class of Base adapter, you can override the methods of base class even in custom adapter.

You should define the constructor as per shown in the above code using super keyword.

Methods of Custom Simple Adapter

Let us understand methods present in the above code one by one.

1. getView()

This method helps us to create the each view for the row item of listview or cell of gridview.

We can inflate the view using the super.getView() method.

Using this inflated view, we can get the access to the various UI widgets present in the row item XML file.

For example, I have inflate the textview using it’s id and also have implemented it’s click scenario.

Similarly, you can access other UI widgets as per your necessity.

2. getCount()

This method will return the number of rows in the listview.

Number of cells in case of gridview.

We can use data set like arraylist to check the total number of count and then just return this number.

3. getItem(int position)

This method will return the data at the specified position.

For example, a hashmap from arraylist of hashmap at the specified position will be returned by this method.

4. getItemId()

This method will return the specific id associated with the data set.

5. getItemViewType(int position)

Listview can have more than two types of row.

For example, a header row, sub header row, normal child row, footer row etc.

When compiler is creating the every row of listview, there should be something which tells it type of the row.

This work will be done by the getItemViewType method.

Custom Simple Adapter With Example

Follow all the below steps to use the custom simple adapter.

Make a fresh new project in the android studio.

Select “Empty activity” while you are selecting the default activity.

Step 1. Download Images

Click the following link to download required images.

Download demonuts_images_listview

When you are complete with the download, copy these images to the res->drawable directory.

Step 2. Layout Files

Now let us make a layout file which will help us to create the UI of listview item.

Create a new file and give it a name like simple_item.xml

This file holds the textview and one imageview.

So every row of listview will have one textview and one imageview.

Now in the activity_main.xml file, write down the following coding lines

Just add the listview in the main file. Nothing else.

Step 3. Custom Adapter Generation

Make a new JAVA class and give it a name CustomSimpleAdapter.java

Code structure for CustomSimpleAdapter.java looks like the below

There is a constructor in this file.

Constructor will get context, arraylist of hashmap (data source), resource (XML file ), from (string array) and to (integer array).

Below line will take care about all the parameters.

Compiler will get the XML view file and it will set automatically to every row.

Similarly, it will set the data, from and to at appropriate place.

Now focus on the getView() method.

Here, compiler will inflate the view from and using this view, it will find the textview by it’s id.

Then, it will set the logic when the user clicks on the textview.

Step 4. Dealing With Main Activity

Source code for MainActivity.java goes as the below

One string array has the names of the cars.

One integer array has the integer reference to the images present in the drawable.

for loop will create the data set which is the arraylist of hashmap.

In the every iteration of for loop, compiler will create one hashmap.

This hashmap will have two children.

One is the name and other is the image.

Now, a string array named “from” has two values, “name”,”image”

These values are same as the two key of the hashmap.

Integer array “to” has the reference to textview and imageview. (R.id.name and R.id.imgView)

Finally, in the object of the Custom Adapter Class, we will pass all these in parameter adn then set this adapter to the listview.

Final result of the above example is

android custom simple adapter

ListView

android custom simple adapter

Click Event

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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