在我的ListView里面
设置了一个复选框CheckBox
而且这个复选框的不可以被聚焦、不可以被点击的
我是在ListView的ItemClickListner里面
设置了点一下ListView的一个Item,那么这个Item的复选框会被选中
其他Item的复选框不被选中
但为什么ListVIew一滚动,有些复选框就莫名其妙的被选中了?
这是Adapter里面的部分代码,东西有点多,但只有复选框是错的,其他内容都没错
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int index = position;
final ViewHolder holder;
OrderData data = datalist.get(position);
String imageUrl = data.getAvatat() + "";
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_order, null);
holder = new ViewHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.cb_item_order);
holder.iv_avatar = (ImageView) convertView
.findViewById(R.id.iv_item_order_avatar);
holder.iv_arrowDown = (ImageView) convertView
.findViewById(R.id.iv_item_order_ishide);
holder.tv_orderCode = (TextView) convertView
.findViewById(R.id.tv_item_order_code);
holder.tv_price = (TextView) convertView
.findViewById(R.id.tv_item_order_price);
holder.tv_status = (TextView) convertView
.findViewById(R.id.tv_item_order_status);
holder.tv_time = (TextView) convertView
.findViewById(R.id.tv_item_order_time);
holder.tv_username = (TextView) convertView
.findViewById(R.id.tv_item_order_username);
holder.layout_food = (LinearLayout) convertView
.findViewById(R.id.layout_order_food);
holder.tv_date = (TextView) convertView
.findViewById(R.id.tv_item_order_data);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// // 设置加载中的View
// mAbImageLoader.setLoadingView(convertView
// .findViewById(R.id.progressBar));
// 图片的下载
// mAbImageLoader.display(holder.iv_avatar, imageUrl);
setData(index, holder);
return convertView;
}
private void setData(int position, ViewHolder view) {
OrderData data = datalist.get(position);
Log.d("setData", "被调用");
view.tv_orderCode.setText(data.getOrdCode() + "");
view.tv_price.setText("¥" + data.getOrdYinFuMoney() + "");
view.tv_username.setText(data.getUserName());
view.iv_arrowDown.setOnClickListener(new Iv_onclick(position));
if (data.getStatus() == 1) {
view.tv_status.setText("正在用餐");
view.tv_status.setTextColor(context.getResources().getColor(
R.color.main));
} else {
view.tv_status.setText("可以收款");
view.tv_status.setTextColor(context.getResources().getColor(
R.color.red));
}
if (data.getIsSelected() == 1) {
view.cb.setChecked(true);
} else {
view.cb.setClickable(false);
}
String time = data.getOrdCreateTime(); // 裁剪时间长度
String time1 = time.substring(0, 11);
String time2 = time.substring(11, 19);
view.tv_date.setText(time1);
view.tv_time.setText(time2);
int count = view.layout_food.getChildCount();
if (count == 0) {
// 添加具体的菜品消费信息
LayoutInflater inflater = LayoutInflater.from(context);
List<CaiData> fooddatas = data.getProdList();
Log.d("看看一个订单的菜数", "" + fooddatas.size());
for (CaiData c : fooddatas) {
View foodview = inflater
.inflate(R.layout.item_order_food, null);
TextView tv_name = (TextView) foodview
.findViewById(R.id.tv_item_orderfood_name);
TextView tv_count = (TextView) foodview
.findViewById(R.id.tv_item_orderfood_count);
TextView tv_price = (TextView) foodview
.findViewById(R.id.tv_item_orderfood_price);
String caiming = c.getCaiming();
caiming = toLength10(caiming);
tv_count.setText("x" + c.getCount());
tv_name.setText(caiming);
tv_price.setText("¥" + (c.getPrice() * c.getCount()));
view.layout_food.addView(foodview);
// mAbImageLoader.display(view.iv_avatar, data.getAvatat()+"");
}
}
// 设置下拉的详细菜品是否可见
if (data.isVisable()) {
view.iv_arrowDown.setImageResource(R.drawable.arrow_up);
view.layout_food.setVisibility(View.VISIBLE);
} else {
view.iv_arrowDown.setImageResource(R.drawable.arrow_down);
view.layout_food.setVisibility(View.GONE);
}
}
public void setSelected(int selected) {
int isSelected = datalist.get(selected).getIsSelected();
if (isSelected == 1) {
Toast.makeText(context, "至少需要勾选一项订单!!", 2000).show();
return;
}
for(int i=0;i<datalist.size();i++){
datalist.get(i).setIsSelected(0);
if(i== selected){
datalist.get(i).setIsSelected(1);
}
}
this.selected = selected;
notifyDataSetChanged();
}
这个是ListView的Item监听器
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
adapter.setSelected(position);
}
});
请问各位,这问题出在哪????
------解决思路----------------------
listview 会复用convertView,直接复用超出屏幕的那个convertview,所以getView里面要做checkbox的赋值操作
if (data.getIsSelected() == 1) {
view.cb.setChecked(true);
} else {
view.cb.setChecked(false);
view.cb.setClickable(false);
}