Universal adapter (2) Extract CommonAdapter
The data and methods in the BaseAdapter are basically the same
getCount(), getItemId(), getItem()
The main thing is that the getView implementation is different,
Common methods will be extracted
<span style="font-size:18px;">package com.example.utils;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.example.magicAdapter.R;
/* * Provide a CommonAdatper * */
public abstract class CommonAdapter<T> extends BaseAdapter {
<span style="white-space:pre"> </span>protected Context context;
<span style="white-space:pre"> </span>protected LayoutInflater minInflater;
<span style="white-space:pre"> </span>protected List<T> mDatas;
protected int layoutId;
<span style="white-space:pre"> </span>public CommonAdapter(Context context,
<span style="white-space:pre"> </span>List<T> mDatas,int layoutId) {
<span style="white-space:pre"> </span>super(a); <span style="white-space:pre"> </span>this.context = context;
<span style="white-space:pre"> </span>this.layoutId =layoutId;
<span style="white-space:pre"> </span>minInflater = LayoutInflater.from(context);
<span style="white-space:pre"> </span>this.mDatas = mDatas;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public int getCount(a) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>return mDatas.size();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public T getItem(int position) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>return mDatas.get(position);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public long getItemId(int position) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>return position;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public View getView(int position, View convertView, ViewGroup parent){
<span style="white-space:pre"> </span>// Get MagicViewHolder first
<span style="white-space:pre"> </span>MagicViewHolder magicViewHolder
<span style="white-space:pre"> </span>= MagicViewHolder.get(context, convertView, parent,
<span style="white-space:pre"> </span>layoutId, position); <span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> // Get the specified view via magicViewHolder and viewid in item and set the value
<span style="white-space:pre"> </span>convert(magicViewHolder, getItem(position));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>// Get the convertView from ViewHolder
<span style="white-space:pre"> </span>return magicViewHolder.getConvertView();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/** * / style="white-space:pre"> */
<span style="white-space:pre"> </span>public abstract void convert(MagicViewHolder magicViewHolder, T t);
}
</span>
Copy the code
Take a look at the simplified MagicAdapter
(1) Inherit custom CommonAdapter
(2) The main implementation of control assignment method convert(); \
package com.example.utils;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.baseAdapter.Bean;
import com.example.magicAdapter.R;
public class MyMagicAdapter extends <span style="color:#ff0000;">CommonAdapter<Bean></span> {
<span style="white-space:pre"> </span>public MyMagicAdapter(Context context, List<Bean> mDatas, int layoutId) {
<span style="white-space:pre"> </span>super(context, mDatas, layoutId);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void convert(MagicViewHolder magicViewHolder, Bean bean) {
<span style="white-space:pre"> </span>// Get the specified view via magicViewHolder and viewid in item and set the value
<span style="white-space:pre"> </span>((TextView)(magicViewHolder.getView(R.id.item_title))).setText(bean.getTitle());
<span style="white-space:pre"> </span>((TextView)(magicViewHolder.getView(R.id.item_desc))).setText(bean.getDesc());
<span style="white-space:pre"> </span>((TextView)(magicViewHolder.getView(R.id.item_time))).setText(bean.getTime());
<span style="white-space:pre"> </span>((TextView)(magicViewHolder.getView(R.id.item_phone))).setText(bean.getPhone());
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
}
Copy the code
\
\