「已注销」 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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛