In this tutorial you will learn about the Android Base Adapter and its application with practical example.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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 parent) { return null; } |
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 :
- 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 2 3 4 |
@Override public int getCount() { return arraylist.size(); } |
- 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 2 3 4 5 6 7 8 9 10 |
@Override public View getView(final int position, View view, ViewGroup parent) { view = LayoutInflater.from(mContext).inflate(R.layout.layout_lv_adapter, null); ImageView mItemPic = (ImageView) view.findViewById(R.id.im_test); TextView mLangName=(TextView)view.findViewById(R.id.tv_langName); return view; } |
- 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 2 3 4 |
@Override public Object getItem(int position) { return arraylist.get(position); } |
- getItemId(int i) : It returns the corresponding to the position item id. It returns long value of item position like below code:
1 2 3 4 |
@Override public long getItemId(int position) { return position; } |
So that was the Overview of Base Adapter Class. You can use it in Grid View , List View, Spinner etc.