Android Simple BaseAdapter – Tutorial

BaseAdapter

Before we share BaseAdapter it is first important to revise Adapter. In android, an adapter is a bridge between UI component and data source that helps us to fill data in the UI component. It holds the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on different views like as list view, grid view, spinner etc. For more customization of views we uses the base adapter. Now lets discuss BaseAdapter class.

  • BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView, GridView, Spinner etc.
  • Whenever you need a customized list in a ListView or customized grids in a GridView you create your own adapter and extend base adapter in that.
  • Base Adapter can be extended to create a custom Adapter for displaying a custom list item.

Following is the example code of custome adapter when we extends the BaseAdapter in that:

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 viewGroup) {

return null;
}

 

Now let’s start a sample code.

Step by Step Tutorial

Step 1: Open Android Studio.

Step 2: Create a new project as follows:

  • If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under ‘Quick Start’ on the right of the dialog.
  • Otherwise, click File in the Android Studio menu bar, then New, New Project.

Step 3: Enter your app name, company domain, and project location, as prompted. Then click Next.

Step 4: Select the form factors you need for your app. If you’re not sure what you need, just select Phone and Tablet. Then click Next.

Step 5: Select Empty Activity in the ‘Add an activity to Mobile’ dialog. Then click Next.

Step 6: Enter the activity name, layout name and title as prompted. The default values are fine. Then click Finish.

After that wait for few seconds. Android Studio starts Gradle and builds of your project.

Step 7: Add ListView control into activity_Main.xml file

<ListView android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

Step 8: Add a new Layout File.

  • Right Click on Layout => New => Layout resource file.
  • Give the name of new layout file (for example itemlist) and click OK button.

you will see a new xml file under layout folder named itemlist.xml.

Step 9:  Now create a custom adapter with the name of MyListAdapter and extend BaseAdapter  and do the following code into MyListAdapter class.

private class MyListAdapter extends BaseAdapter {

    @Override
    public int getCount(){

        if(BrandName != null && BrandName.length != 0 )
        {
            return BrandName.length;
        }
           return 0;
    }

    @Override
    public  Object getItem (int position)
    {
        return BrandName[position];
    }

    @Override
    public long getItemId (int position)
    {
        return  position;
    }

    private  class ViewHolder
    {
        TextView txtBrandName;
        int ref;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            convertView = inflater.inflate(R.layout.itemlist, null);
            holder.txtBrandName = (TextView) convertView.findViewById(R.id.itemName);

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
               }

        holder.ref=position;
        holder.txtBrandName.setText(BrandName[position]);

        return convertView;
}

Step 9: Now declare the object of ListView and MyListAdapter within onCreate() method and assign adapter to Listview.

ListView mList = (ListView) findViewById(R.id.list);
MyListAdapter mAdapter = new MyListAdapter();
mList.setAdapter(mAdapter);

Following is the full source code of this tutorial:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.suraj.baseadaptertutorial.MainActivity">

    <ListView android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
    
</RelativeLayout>

itemlist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/itemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

Custom Adapter extend BaseAdapter Code

private class MyListAdapter extends BaseAdapter {

    @Override
    public int getCount(){

        if(BrandName != null && BrandName.length != 0 )
        {
            return BrandName.length;
        }
           return 0;
    }

    @Override
    public  Object getItem (int position)
    {
        return BrandName[position];
    }

    @Override
    public long getItemId (int position)
    {
        return  position;
    }

    private  class ViewHolder
    {
        TextView txtBrandName;
        int ref;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            convertView = inflater.inflate(R.layout.itemlist, null);
            holder.txtBrandName = (TextView) convertView.findViewById(R.id.itemName);

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
               }

        holder.ref=position;
        holder.txtBrandName.setText(BrandName[position]);

        return convertView;
}

Full source code of MainActivity.java file

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.zip.Inflater;

import static java.sql.Types.NULL;

public class MainActivity extends AppCompatActivity {

    private String[] BrandName = new String[]{"iPhone", "Samsung", "Nokia", "HTC", "Moto"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView mList = (ListView) findViewById(R.id.list);
        MyListAdapter mAdapter = new MyListAdapter();
        mList.setAdapter(mAdapter);

    }




    private class MyListAdapter extends BaseAdapter {

        @Override
        public int getCount(){

            if(BrandName != null && BrandName.length != 0 )
            {
                return BrandName.length;
            }
               return 0;
        }

        @Override
        public  Object getItem (int position)
        {
            return BrandName[position];
        }

        @Override
        public long getItemId (int position)
        {
            return  position;
        }

        private  class ViewHolder
        {
            TextView txtBrandName;
            int ref;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                LayoutInflater inflater = MainActivity.this.getLayoutInflater();
                convertView = inflater.inflate(R.layout.itemlist, null);
                holder.txtBrandName = (TextView) convertView.findViewById(R.id.itemName);

                convertView.setTag(holder);
            }
            else{
                holder = (ViewHolder) convertView.getTag();
                   }

            holder.ref=position;
            holder.txtBrandName.setText(BrandName[position]);

            return convertView;
    }
}
}

 

 

Post a comment

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