您的位置:首页 > 移动开发 > Android开发

Android 中一个封装还不错的BasicAdapter

2016-04-11 21:49 423 查看
Android 中一个简单封装的BasicAdapter

平时我们在项目中,经常使用adapter,自己封装了一下,感觉用起来还不错,在使用过程中,直接传递,不用考虑list是否为null,代码如下

package com.example.adapter;

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

import java.util.ArrayList;

public class BasicAdapter<T> extends BaseAdapter {

protected ArrayList<T> listData;
protected Context context;

public BasicAdapter(Context context, ArrayList<T> list) {
this.context = context;
this.listData= list;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
if (list != null)
return list.size();
return 0;
}
public void addItem(List<T> list) {
this.listData.addAll(list);
notifyDataSetChanged();
}

public void refreshItem(List<T> list) {
this.listData.clear();
this.listData.addAll(list);
notifyDataSetChanged();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub

if (position == getCount() - 1 || list == null)
return null;

return list.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

return null;
}

}


并且当adapter去继承BasicAdapter的时候,重写getview(),用一种新的思路,使用构造函数,举个例子:

public class CompareItemAdapter extends BasicAdapter<DeviceCheck> {
public CompareItemAdapter(Context context, ArrayList<DeviceCheck> list) {
super(context, list);
}

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

if (convertView == null) {
convertView = View.inflate(context, R.layout.xlistitem_item_compare, null);
}
return convertView;
}

static class ViewHolder {
public TextView id_tv_name;
public TextView id_tv_input_by;
public TextView id_tv_device_data;
public TextView id_tv_percent;

public ViewHolder(View convertView) {
id_tv_name = (TextView) convertView.findViewById(R.id.id_tv_name);
id_tv_input_by = (TextView) convertView.findViewById(R.id.id_tv_input_by);
id_tv_device_data = (TextView) convertView.findViewById(R.id.id_tv_device_data);
id_tv_percent = (TextView) convertView.findViewById(R.id.id_tv_percent);
}

public static ViewHolder getHolder(View convertView) {
ViewHolder holder = (ViewHolder) convertView.getTag();

if (holder == null) {
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}

return holder;
}
}
}


再看看我们常规的写法,是这样子的:

public class YearItemAdapter extends BaseAdapter {
private ArrayList<Year> listRank;
private Context context;
private int type;

public YearItemAdapter(ArrayList<Year> listRank, Context context, int type) {
this.listRank = listRank;
this.context = context;
this.type = type;
}

public YearItemAdapter(Context context) {
this.listRank = new ArrayList<Year>();
this.context = context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
if (listRank!=null){
return listRank.size();
}
return 0;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return listRank.get(arg0);
}

public void addItem(List<Year> list) {
this.listRank.addAll(list);
notifyDataSetChanged();
}

public void refreshItem(List<Year> list) {
this.listRank.clear();
this.listRank.addAll(list);
notifyDataSetChanged();
}

public List<Year> getList() {
return listRank;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub

ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.xlistitem_item_year, null);
holder = new ViewHolder();
holder.id_tv_rank_item = (TextView) convertView.findViewById(R.id.id_tv_rank_item);
holder.id_tv_name = (TextView) convertView.findViewById(R.id.id_tv_name);
holder.id_tv_pollutecount = (TextView) convertView.findViewById(R.id.id_tv_pollutecount);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

Year nb = listRank.get(position);
holder.id_tv_rank_item.setText(String.format("%d", position + 1));
holder.id_tv_name.setText(String.valueOf(nb.getYEAR() + "年"));
switch (type) {
case 1:
holder.id_tv_pollutecount.setText(String.valueOf(nb.getJDC_DL()));
break;
case 2:
holder.id_tv_pollutecount.setText(String.valueOf(nb.getKC_DL()));
break;
case 3:
holder.id_tv_pollutecount.setText(String.valueOf(nb.getHC_DL()));
break;
case 4:
holder.id_tv_pollutecount.setText(String.valueOf(nb.getJDC_CS()));
break;
case 5:
holder.id_tv_pollutecount.setText(String.valueOf(nb.getYJD()));
break;

}

return convertView;
}

public static class ViewHolder {
public TextView id_tv_rank_item;
public TextView id_tv_name;
public TextView id_tv_pollutecount;

}
}


憋说话,用心去感受O(∩_∩)O哈哈~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: