Android Base Adapter

Android Base Adapter

Adapter is a bridge between UI and data source, It helps us to fill data in the UI components. It pulls data from database or an array. After pulling data from database or an array, it sends data to adapter view and adapter view send it to view. In final stage view shows data on different Views Like ListView, GridView, Spinner , RecyclerView etc. An Adapter provide us simple view, If we need to customize it so we have to use Base Adapter.

BaseAdapter is very generic adapter which allows you to do pretty much whatever you want. Base Adapter is common base class of a general implementation of an Adapter. It generally used in ListView, GridView, Spinner etc. Base Adapter extends an object class and implement ListAdapter and SpinnerAdapter.

Lets understand it by Coding, how it works and which methods are overrided by it.

Firstly we need to create a class Like I did CustomAdapter is a class and then you have to extends BaseAdapter class.

Lets discuss all the functions briefly :

  1. getCount() : This function is used to returns total number count of items which are going to display in list. It counts the number from List Size like “list.size();”. Here I  have used array list so like below code you can return count of total items.

  1. getView(int I, View view, ViewGroup parent) : This function is called when items are going to display in ListView or GridView or Spinner. In this function we bind the Layout for items using LayoutInflater class and then fetch the views like TextView, EditText, ImageView etc.
    In below code you will get that how it will work:

  1. getItem(int i) : This is used to get the data items associated with the specified position in the data set. Below is code for this, It returns the arraylist’s item according to position.

  1. getItemId(int i) : It returns the corresponding to the position item id. It returns long value of item position like below code:

So that was the Overview of Base Adapter Class. You can use it in Grid View , List View, Spinner etc.