Android入门之利用Spinner实现弹出选择对话框

  package org.mk.android.demospinneradapter;

  import android.content.Context;

  import android.util.Log;

  import android.util.SparseArray;

  import android.view.LayoutInflater;

  import android.view.View;

  import android.view.ViewGroup;

  import android.widget.BaseAdapter;

  import android.widget.ImageView;

  import android.widget.TextView;

  import java.util.ArrayList;

  import java.util.List;

  public abstract class GenericAdapter extends BaseAdapter {

  private List data;

  private int layoutRes;

  public GenericAdapter() {

  }

  public GenericAdapter(List data, int layoutRes) {

  this.data = data;

  this.layoutRes = layoutRes;

  }

  @Override

  public int getCount() {

  Log.i("app",">>>>>>data.size: "+data.size());

  if(data!=null) {

  return data.size();

  }

  return 0;

  }

  @Override

  public T getItem(int position) {

  return data.get(position);

  }

  @Override

  public long getItemId(int position) {

  return position;

  }

  @Override

  public View getView(int position, View convertView, ViewGroup parent) {

  Log.i("app",">>>>>>into getView");

  ViewHolder holder = ViewHolder.bind(parent.getContext(), convertView, parent, layoutRes

  , position);

  bindView(holder, getItem(position));

  return holder.getItemView();

  }

  //添加一个元素

  public void add(T item) {

  if (data == null) {

  data = new ArrayList<>();

  }

  data.add(item);

  notifyDataSetChanged();

  }

  //往特定位置,添加一个元素

  public void add(int position,T item){

  if (data == null) {

  data = new ArrayList<>();

  }

  data.add(position, item);

  notifyDataSetChanged();

  }

  public void remove(T item) {

  if(data != null) {

  data.remove(item);

  }

  notifyDataSetChanged();

  }

  public void remove(int position) {

  if(data != null) {

  data.remove(position);

  }

  notifyDataSetChanged();

  }

  public void clear() {

  if(data != null) {

  data.clear();

  }

  notifyDataSetChanged();

  }

  public abstract void bindView(ViewHolder holder, T obj);

  public static class ViewHolder {

  private SparseArray mViews; //存储ListView 的 item中的View

  private View item; //存放convertView

  private int position; //游标

  private Context context; //Context上下文

  //构造方法,完成相关初始化

  private ViewHolder(Context context, ViewGroup parent, int layoutRes) {

  mViews = new SparseArray<>();

  this.context = context;

  View convertView = LayoutInflater.from(context).inflate(layoutRes, parent,false);

  convertView.setTag(this);

  item = convertView;

  }

  public static ViewHolder bind(Context context, View convertView, ViewGroup parent,

  int layoutRes, int position) {

  ViewHolder holder;

  if(convertView == null) {

  holder = new ViewHolder(context, parent, layoutRes);

  } else {

  holder = (ViewHolder) convertView.getTag();

  holder.item = convertView;

  }

  holder.position = position;

  return holder;

  }

  public T getView(int id) {

  T t = (T) mViews.get(id);

  if(t == null) {

  t = (T) item.findViewById(id);

  mViews.put(id, t);

  }

  return t;

  }

  /**

  * 获取当前条目

  */

  public View getItemView() {

  return item;

  }

  /**

  * 获取条目位置

  */

  public int getItemPosition() {

  return position;

  }

  /**

  * 设置文字

  */

  public ViewHolder setText(int id, CharSequence text) {

  View view = getView(id);

  if(view instanceof TextView) {

  ((TextView) view).setText(text);

  }

  return this;

  }

  /**

  * 设置图片

  */

  public ViewHolder setImageResource(int id, int drawableRes) {

  View view = getView(id);

  if(view instanceof ImageView) {

  ((ImageView) view).setImageResource(drawableRes);

  } else {

  view.setBackgroundResource(drawableRes);

  }

  return this;

  }

  /**

  * 设置标签

  */

  public ViewHolder setTag(int id, Object obj) {

  getView(id).setTag(obj);

  return this;

  }

  public ImageView iconImg;

  public TextView iconText;

  }

  }