「已注销」 2016-01-22 13:48 采纳率: 25%
浏览 2591

为什么我一调用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 2016-01-22 15:27
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题