在Android开发中经常会遇到Listview的item中有子控件,并且需要与其中的子控件交互,一般的控件还好,但是像Checkbox这类老大难的控件,出现的问题也是众所周知的错位。举个例子说,我勾选Listview中的第一个条目中Checkbox,实际上选中的不一定是第一个,这样在进行数据操作时必然是乱糟糟的一团。如下图这样的需求
勾选的同时还要进行数据库操作,因此必须要解决这错位问题
在开发中遇到了这样的操作,通过搜索与总结,总算弄出了解决方案,不废话了,上代码:
package cn.zmit.tourguide.adapter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import cn.zmit.tourguide.R;
import cn.zmit.tourguide.engine.OrderLunchTask;
import cn.zmit.tourguide.engine.TeamTask;
import cn.zmit.tourguide.engine.TeamTask.TeamDateMode;
import cn.zmit.tourguide.entity.OrderLunchData;
import cn.zmit.tourguide.entity.TeamData;
import cn.zmit.tourguide.entity.TouristData;
import cn.zmit.tourguide.inter.OnQueryOrderLunchInfoListener;
import cn.zmit.tourguide.inter.OperationListener;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.robinframe.common.utils.DialogUtils;
import com.robinframe.common.utils.SystemUtils;
/**
* 订餐界面列表适配器
*
* @author Robin time 2014-12-18 15:31:43
*
*/
public class OrderLunchListAdapter extends BaseAdapter {
/**
* 上下文
*/
private Context context;
/**
* 游客信息集合
*/
private List touristDatas;
/**
* 布局加载器
*/
private LayoutInflater inflater;
/**
* 进度条Dialog
*/
private ProgressDialog progressDialog;
/**
* 团次信息对象
*/
private TeamData teamData;
/**
* 更新操作回调接口
*/
private OperationListener operationListener;
Set selectedSet = new HashSet(); // 记住单击选择的状态集合
public OrderLunchListAdapter(Context context, List touristDatas,TeamData teamData,OperationListener operationListener) {
inflater = LayoutInflater.from(context);
this.context = context;
this.touristDatas = touristDatas;
this.teamData =teamData;
this.operationListener =operationListener;
progressDialog = DialogUtils.getProgressDialog(context);
}
public void AddTouristInfo(List touristDatas) {
for (TouristData touristData : touristDatas) {
this.touristDatas.add(touristData);
}
}
@Override
public int getCount() {
return touristDatas.size();
}
@Override
public Object getItem(int position) {
return touristDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.roll_call_listview_item,
null);
holder = new ViewHolder();
ViewUtils.inject(holder, convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_tourist_name.setText(touristDatas.get(position).getName()
+ "");
holder.tv_tourist_phone.setText(touristDatas.get(position).getPhone());
if (touristDatas.get(position).getGender().equals("0")) {
holder.image_tourist_sex.setBackgroundResource(R.drawable.man);
} else {
holder.image_tourist_sex.setBackgroundResource(R.drawable.woman);
}
new OrderLunchTask().queryOrderLunchInfoByTeamId(context, teamData.getTeamId(), progressDialog, false, new OnQueryOrderLunchInfoListener() {
@Override
public void OnQueryOrderLunchInfoSuccess(
List orderLunchDatas) {
if (orderLunchDatas!=null) {
String touristId=touristDatas.get(position).getTouristId(); //当前Listview位置游客id
try {
OrderLunchData orderLunchData=DbUtils.create(context).findFirst(Selector.from(OrderLunchData.class).where("touristId", "=", touristId)); //根据当前Listview位置的游客id找到OrderLunchData对象
LogUtils.i("订餐数据:"+orderLunchData.toString());
if (orderLunchData.isOrderLunched()) {
holder.cb_roll_call.setButtonDrawable(R.drawable.roll_call_check_box_selector);
holder.cb_roll_call.setChecked(true);
holder.tv_arrive_status.setText("已订餐");
}else {
holder.cb_roll_call.setButtonDrawable(R.drawable.roll_call_check_box_selector);
holder.cb_roll_call.setChecked(false);
holder.tv_arrive_status.setText("未订餐");
}
} catch (DbException e) {
e.printStackTrace();
}
}else {
holder.cb_roll_call.setButtonDrawable(R.drawable.roll_call_check_box_selector);
holder.cb_roll_call.setChecked(false);
holder.tv_arrive_status.setText("未订餐");
}
}
});
holder.cb_roll_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LogUtils.i("点击位置:"+position+"");
CheckBox cb = (CheckBox) view;
if (cb.isChecked()) {
selectedSet.add((Object) getItem(position)); //记住选择的对象,加入selectedSet集合
updateCheckBoxStatus(position, holder); //更新Checkbox状态
} else {
selectedSet.remove((Object) getItem(position)); //从selectedSet集合中移除选择的对象
updateCheckBoxStatus(position, holder); //更新Checkbox状态
}
}
});
return convertView;
}
/**
* 更新Checkbox状态
* @param position Listview位置
* @param holder ViewHolder
*/
private void updateCheckBoxStatus(final int position,
final ViewHolder holder) {
Object b = (Object) getItem(position);
if (b != null) {
if (selectedSet.contains(b)) {
holder.cb_roll_call
.setButtonDrawable(R.drawable.roll_call_check_box_selector);
holder.cb_roll_call.setChecked(true);
holder.tv_arrive_status.setText("已订餐");
String touristId=touristDatas.get(position).getTouristId();
new OrderLunchTask().updateOrderStatus(context, touristId, true,progressDialog,false,new OperationListener() { // 更新订餐数据表订餐状态
@Override
public void OperationSuccess() {
operationListener.OperationSuccess(); //更新操作成功回调
}
});
} else {
holder.cb_roll_call
.setButtonDrawable(R.drawable.roll_call_check_box_selector);
holder.cb_roll_call.setChecked(false);
holder.tv_arrive_status.setText("未订餐");
String touristId=touristDatas.get(position).getTouristId();
new OrderLunchTask().updateOrderStatus(context, touristId, false,progressDialog,false,new OperationListener() { // 更新订餐数据表订餐状态
@Override
public void OperationSuccess() {
operationListener.OperationSuccess(); //更新操作成功回调
}
});
}
}
/**
* 更新订餐时间
*/
new TeamTask().updateTeamDate(context, teamData.getTeamId(), TeamDateMode.ORDER_LUNCH_DATE,SystemUtils.getDataTime("MM.dd"),"", progressDialog, false, new OperationListener() {
@Override
public void OperationSuccess() {
}
});
}
static class ViewHolder {
@ViewInject(R.id.tv_tourist_name)
TextView tv_tourist_name;
@ViewInject(R.id.tv_tourist_phone)
TextView tv_tourist_phone;
@ViewInject(R.id.image_tourist_sex)
ImageView image_tourist_sex;
@ViewInject(R.id.cb_roll_call)
CheckBox cb_roll_call;
@ViewInject(R.id.tv_arrive_status)
TextView tv_arrive_status;
}
}
原理很简单,一般对Checkbox操作是要设置onCheckChangeListener,这里不再设置onCheckChangeListener,而是对Checkbox设置点击事件onClickListener,然后判断其是否为勾选状态,若不为勾选,则把当前对象加入我们声明的set集合里面,反之,若已勾选状态,则把对象从set集合中移除,然后调用更新Checkbox状态的方法,在方法里面判断set集合是否包含对应的对象,若包含,则设为true状态,反之为false
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/5892.html
微信打赏
支付宝打赏
感谢您对作者wangbin的打赏,我们会更加努力! 如果您想成为作者,请点我