import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Map<String, Object>> mData;
public static Map<Integer, Boolean> isSelected; //是否被选中
public static Map<Integer, Boolean> isFail; //保存结果,测试是否失败
public static Map<Integer, Boolean> ischeck; //是否已测试
private boolean isSelectAll;
private static String title[]={
"LCD",
"KeyPad",
"Camera LED",
"LCD Backlight",
"Key Backlight",
"Vibrator",
"Front camera",
"Back camera",
"Battery temperature",
"Receiver",
"Speaker",
"Mic",
"SIM",
"USB",
"Headset",
"FM Radio",
"Blue tooth",
"WIFI",
"GSensor",
"ECompass",
"Light_Sensor",
"Proximity_sensor",
"GPS",
};
public MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
init();
}
//初始化
private void init()
{
mData=new ArrayList<Map<String, Object>>();
isSelectAll = false;
for (int i = 0; i < title.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", title[i]);
mData.add(map);
}
//这儿定义isSelected这个map是记录每个listitem的状态,初始状态全部为false。
isSelected = new HashMap<Integer, Boolean>();
isFail = new HashMap<Integer, Boolean>();
ischeck = new HashMap<Integer, Boolean>();
for (int i = 0; i < mData.size(); i++) {
isSelected.put(i, false); //默认为未选中
isFail.put(i, true); //初始化,默认成功
ischeck.put(i, false); //初始化,没测试
}
}
@Override
public int getCount()
{
return mData.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
//convertView为null的时候初始化convertView。
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.listmenu, null);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.cBox = (CheckBox) convertView.findViewById(R.id.cb);
convertView.setTag(holder);
// 设置是否被全选
if (this.isSelectAll == true) {
holder.cBox.setChecked(true);
} else {
holder.cBox.setChecked(false);
}
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(mData.get(position).get("title").toString());
holder.cBox.setChecked(isSelected.get(position));
if(ischeck.get(position)){
if(isFail.get(position)){
holder.img.setBackgroundResource(R.drawable.fail);
}else{
holder.img.setBackgroundResource(R.drawable.pass);
}
}else{
holder.img.setBackgroundResource(R.drawable.none);
}
return convertView;
}
//是否全选
public boolean isSelectAll() {
return isSelectAll;
}
//设置全选
public void setSelectAll(boolean isSelectAll) {
this.isSelectAll = isSelectAll;
}
public final class ViewHolder {
public ImageView img;
public TextView title;
public CheckBox cBox;
}
}
一个android带可变图标以及checkbox的ListView的Adapter的实现,用于工厂测试
最新推荐文章于 2022-07-01 22:35:16 发布