WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

BaseAdapter Android


This adapter is very important in android it is bridge between UI component and data source.
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 such as ListView, GridView, Spinner .

Custom Adapter when we extends the BaseAdapter

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;
}

getView(int i, View view, ViewGroup viewGroup):

When the list item view is ready to be displayed is called getView.
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_gridview, null);//set layout for displaying items
ImageView Photo = (ImageView) view.findViewById(R.id.photo);//get id for image view
icon.setImageResource(flags[i]);//set image of the item’s
return view;
}

getItem(int i):

This function is used to Get the data item associated with the specified position in the data set to obtain the corresponding data of the specific location in the collection of data items

@Override
public Object getItem(int i) {
return arrayList.get(i);
}

getCount():

This function is return total no. of items to be displayed in a list.
@Override
public int getCount() {
int count=arrayList.size(); //counts the total number of elements from the arrayList
return count;//returns the total count to adapter
}

BaseAdapter Example.

Create First project in Android Studio


Select File -> New -> New Project. Fill the forms and click "Finish" button.

Open app -> res -> layout -> activity_main.xml

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

  <ListView
        android:id="@+id/thisListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      
        android:dividerHeight="5dp"
        android:footerDividersEnabled="false" />
</LinearLayout>

we can make activity_listview.xml

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

   <ImageView
        android:id="@+id/photo"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="@dimen/activity_horizontal_margin"
        android:textColor="#a00" />
</LinearLayout>

then we open app -> java-> package -> MainActivity.java

package example.welookups.baseadapterexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    ListView simpleList;
    String fruitsList[] = {"Feijoa", "Elderberries", "Grapes", "Guava", "Lychee", "Mulberries"};
    int flags[] = {R.drawable.Feijoa, R.drawable.Elderberries, R.drawable.Grapes, R.drawable.Guava, R.drawable.Lychee, R.drawable.Mulberries};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleList = (ListView) findViewById(R.id.simpleListView);
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), fruitsList, flags);
        simpleList.setAdapter(customAdapter);
    }
}

Final we can add new class CustomAdapter.java

package example.welookups.baseadapterexample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class CustomAdapter extends BaseAdapter {
    Context context;
    String  fruitsList[];
    int flags[];
    LayoutInflater inflter;

    public CustomAdapter(Context applicationContext, String[]  fruitsList, int[] flags) {
        this.context = context;
        this.countryList = countryList;
        this.flags = flags;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return  fruitsList.length;
    }

    @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) {
        view = inflter.inflate(R.layout.activity_listview, null);
        TextView country = (TextView) view.findViewById(R.id.textView);
        ImageView photo = (ImageView) view.findViewById(R.id.photo);
        country.setText(fruitsList[i]);
        icon.setImageResource(flags[i]);
        return view;
    }
}