Create a Generic Base Adapter?

AndroidGeek picture AndroidGeek · Jul 4, 2014 · Viewed 8.7k times · Source

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

Answer

AndroidGeek picture AndroidGeek · Jul 13, 2016

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