问题描述
- 为什么我一调用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; }
}
解决方案
http://www.itstrike.cn/Question/1ba8ea59-d3a8-4bea-ab2a-0531e4b6f2f7.html
解决方案二:
你是不是在子线程中进行的,必须在主线程才行
时间: 2024-10-25 17:25:40