what is BaseAdapter and how to Custom List View in Android?


In the previous Blog, I have already talked to you about the adapter and how it works so if you don't know what is adapter please watch the previous blog.
As you know Adapter in android works as a bridge or medium between layout and data source.
An adapter is inbuilt in Android and every adapter has a parent adapter name BaseAdapter so BaseAdapter is the parent Adapter of all the Adapters.
mainly you have heard two types of the adapter one is ArrayAdapter and other is  BaseAdapter but ArrayAdapter is Also the Implementation of Base Adapter so ArrayAdapter also extends to BaseAdapter you can see it as below.



So everything that we do in Adapter Belongs to BaseAdapter Class.
most use Adapter-
                  1.ArrayAdapter
                  2.BaseAdapter
                  3.Custom ArrayAdapter
                  4.SimpleAdapter
                  5. Custom SimpleAdapter
I have already told how to work with ArrayAdapter in this blog I will tell you about BaseAdapter and how to make a custom list view with the help of BaseAdapter.

A process to create CustomListView-

1. Create a layout of the adapter (the custom layout that you want to show)-
I have taken an image view and a text view on it. So create by going layout and rightclick>createnew>drawableresourceFile create a file I name it adapter_layout .

2. Then create a ListView inside activity_main.xml like below-
3. After Layout part is done make a java class name CustomAdapter.java and extend it
with BaseAdapter class. Now implements All its methods.
After that, you have to edit these methods according to you.

getCount();  - In this, you have to return Total size of the ArrayList or what you pass.


getItem(int position)  - This returns the item of particular position if you want to
fetch particular data otherwise return null.


getItemId(int position) - This will return the Position or Item id whenever it needed.


getView(int position, View view, ViewGroup parent) -  In this You Have to set Layout of AdapterLayout. you can set a layout by below code-
                    view = inflter.inflate(R.layout.adapter_layout, null);


4. Now Make a CountryListArray and FlagsArray and find ListView Then create Adapter Instance and set in the ListView like Below.


5. Now make Adapter Like below make Constructor for custom Adapter and set View and set flag and County name at particular Position. 

6. Now, this type you can make a custom listview with Help Of BaseAdapter.


Hope This Blog will HelpFul for You if you have any doubt whole Project Code
is Given Below-
         
                                  ProjectCode- ClickMe

Post a Comment

0 Comments