2 candy rainbow candy_rainbow 于 2016.01.22 17:07 提问

关于安卓ListView与CheckBox结合

这是我走定义的适配器,为什么说在设置check状态的时候是空指针呀

public class MyAdapter extends BaseAdapter {

private ArrayList<Item> list;
//记录和控制选中状态
private static HashMap<Integer,Boolean> isSelected;
 //上下文
private Context context;
 //为子项导入布局
private LayoutInflater inflater;

public MyAdapter(ArrayList<Item> list,Context context) {
    this.list = list;
    this.context = context;
    inflater = LayoutInflater.from(context);
    isSelected = new HashMap<Integer, Boolean>();
    //初始化状态
    initDate();
}

private void initDate() {
    for(int i=0; i<list.size();i++) {
        getIsSelected().put(i,false);
    }
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

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

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

@Override
public View getView(final int position, View convertview,ViewGroup parent) {
    ViewHolder holder ;
    if(convertview == null) {
        // 获得ViewHolder对象
        holder = new ViewHolder();
        // 导入布局并赋值给convertview
        convertview = inflater.inflate(R.layout.item_layout, null);
        holder.project_name = (TextView) convertview.findViewById(R.id.project_name);
        holder.project_result = (TextView) convertview.findViewById(R.id.project_result);
        holder.project_resultval = (TextView) convertview.findViewById(R.id.project_resultval);
        holder.cbox = (CheckBox) convertview.findViewById(R.id.box);
        holder.cbox.setOnClickListener(new View.OnClickListener() {        
            public void onClick(View v) { 
                if (isSelected.get(position)) { 
                    isSelected.put(position, false); 
                    setIsSelected(isSelected); 
                } else { 
                    isSelected.put(position, true); 
                    setIsSelected(isSelected); 
                } 
            } 
        }); 
        // 为view设置标签
        convertview.setTag(holder);
    } else {
        holder = (ViewHolder) convertview.getTag();
    }
    holder.project_name.setText(list.get(position).getproject_name());
    holder.project_result.setText(list.get(position).getproject_result());
    holder.project_resultval.setText(list.get(position).getproject_resultval());
    // 根据isSelected来设置checkbox的选中状况
    holder.cbox.setChecked(getIsSelected().get(position));
    return convertview;
}

public static HashMap<Integer,Boolean> getIsSelected() {
    return isSelected;
}

public static void setIsSelected(HashMap<Integer,Boolean> isSelected) {
    MyAdapter.isSelected = isSelected;
}

public final class ViewHolder {
    public TextView project_name;
    public TextView project_result;
    public TextView project_resultval;
    public CheckBox cbox;
}

}

图片说明

3个回答

devmiao
devmiao   Ds   Rxr 2016.01.22 23:44
lxk_1993
lxk_1993   Rxr 2016.01.23 11:47

试试把点击事件放赋值那里

crazy1235
crazy1235   Ds   Rxr 2016.02.01 13:33
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!