一个android带可变图标以及checkbox的ListView的Adapter的实现,用于工厂测试

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;    
	}  
	
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值