Create a Generic Base Adapter?

I am interested in creating a generic BaseAdapter which will apply to any ListView having any list_item . And it will set the item_row for the list_view.

    public class GenericAdapter extends BaseAdapter
    {

    Context context;

    ArrayList<HashMap<String, String>> list;

    LayoutInflater inflater;

    public GenericAdapter (Context context, ArrayList<HashMap<String, String>> list) 
        {
            this.context = context;

            this.list = list;

            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

        @Override
        public int getCount()
        {
            return list.size();
        }

        @Override
        public Object getItem(int position)
        {   
            return list.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;

          if(view == null)
           {
            view = inflater.inflate(R.layout.item_of_any_list, parent, false);

// i have to do something here like there may be a method which must be call 

           }

            /*HashMap<String, String> map = list.get(position);

            TextView textViewName = (TextView) view.findViewById(R.id.name);

            TextView textViewDate = (TextView) view.findViewById(R.id.date);

            //TextView textViewDesc = (TextView) view.findViewById(R.id.desc);

            textViewName.setText(map.get("name"));

            textViewDate.setText(map.get("date"));

            //textViewDesc.setText(map.get("description"));
    */      
            return view;
        }

I think i have to create an interface that's get and set the item_row for individual item. But i don't know it may be possible . Can someone tell me how i can achieve it.

My idea is to create an interface and GenericAdapter implements that interface. In that interface there is a method to get and set the item_row. Any class which extends GenericAdapter must implement that method. it will help us make a generic adapter.

When we extend that adapter we don't need to write all code like getCount, getItem,getItemId, we just have to override a function which is to inflateLayout. which must be inside getView

Thanks in Advance


Solution 1:

Answering my own Question after two years.

In order to make Generic Adapter you need abstract methods in parent class which should be implemented by child. So GenericAdapter must be Abstract and create abstract methods for data you want to change.

I have different types of ArrayList to show in Adapter only it's model class is different. So I decided to write a genericAdapter. This is for recyclerview if someOne interested for listview. Let me know.

GenericAdapter

public abstract class GenericAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ArrayList<T> items;

    public abstract RecyclerView.ViewHolder setViewHolder(ViewGroup parent);

    public abstract void onBindData(RecyclerView.ViewHolder holder, T val);

    public GenericAdapter(Context context, ArrayList<T> items){
        this.context = context;
        this.items = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = setViewHolder(parent);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindData(holder,items.get(position));
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public void addItems( ArrayList<T> savedCardItemz){
        items = savedCardItemz;
        this.notifyDataSetChanged();
    }

    public T getItem(int position){
        return items.get(position);
    }
}

And I use it like

     adapter = new GenericAdapter<DataModel>(context,modelList) {
        @Override
        public RecyclerView.ViewHolder setViewHolder(ViewGroup parent) {
            final View view =            LayoutInflater.from(context).inflate(R.layout.item_view_holder, parent, false);
            ItemViewHolder viewHolder = new ItemViewHolder(context, view);
            return viewHolder;
        }

        @Override
        public void onBindData(RecyclerView.ViewHolder holder1, DataModel val) {
                DataModel userModel = val;

                ItemViewHolder holder = (ItemViewHolder)holder1;
                holder.name.setText(userModel.getName());
                holder.fatherName.setText(userModel.getFatherName());
        }
    };
    mRecyclerView.setAdapter(adapter);

RecyclerView with Search sort functionality

Solution 2:

I have created a generic adapter, the aar and the sources are at this url:

it's easy usage:

RecyclerAdapter<Customer> mAdapter = new RecyclerAdapter(customerList, CustomerViewHolder.class);

see Url link more details

Solution 3:

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

import java.util.List;

public abstract class GenericAdapter<T> extends BaseAdapter {
    LayoutInflater layoutInflater;
    Context context;
    private List<T> tList;



public GenericAdapter(Context context, List<T> tList) {
        this.context = context;
        this.tList = tList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (tList == null)
            return 0;
        return tList.size();
    }

    @Override
    public Object getItem(int position) {
        return tList.get(position);
    }


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

    public abstract View getMyView(int position, View convertView, ViewGroup parent,T t);

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getMyView(position, convertView, parent,tList.get(position));
    }

}

and you can use it like

spnDistrict.setAdapter(new GenericAdapter<Institutions>(getContext(), institutionsList) {
            @Override
            public View getMyView(int position, View convertView, ViewGroup parent, Institutions institutions) {
                TextView textView = (TextView) convertView;
                if (textView == null) {
                    textView = (TextView) getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null);
                    textView.setTextColor(getContext().getResources().getColor(R.color.black));
                }
                textView.setText(institutions.getName());
                return textView;
            }
        });