2 candy rainbow candy_rainbow 于 2016.01.22 21:48 提问

为什么我一调用mAdapter.notifyDataSetChanged();就崩溃

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;
}

}

2个回答

devmiao
devmiao   Ds   Rxr 2016.01.22 23:27
Mr_dsw
Mr_dsw   Ds   Rxr 2016.01.23 12:23

你是不是在子线程中进行的,必须在主线程才行

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