嗨,我是 Android 开发人员。在此我提到了我的代码。我在自定义列表视图中添加了复选框,我正在使用 Base Adapter。选中复选框时,该项目只选择了非常好,但在选中复选框后滚动时自动取消选中。如何解决这个问题。请给我解决方案。
class LabOrderListAdapter extends BaseAdapter {
LayoutInflater mInflater;
ViewHolder holder;
Boolean checkboxstate[];
Context context;
// DecimalFormat df = new DecimalFormat("#.##");
public LabOrderListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
checkboxstate=new Boolean[observationDetailsList.size()];
for(int i=0;i<checkboxstate.length;i++)
checkboxstate[i]=false;
this.context = context;
}
public int getCount() {
if (observationDetailsList != null) {
Log.i(this.toString(), "observationDetailsList"+observationDetailsList.size());
return observationDetailsList.size();
}
return 0;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.lab_order_listitem, null);
}
if (v != null) {
holder = new ViewHolder();
holder.txt_Order_code = (TextView) v
.findViewById(R.id.orderingcode);
holder.txt_ProcedureCode = (TextView) v
.findViewById(R.id.procedurecode);
holder.txt_Laboratory = (TextView) v
.findViewById(R.id.Laboratory);
holder.billtypeButton = (AprimaButton) v.findViewById(R.id.billtype);
holder.txt_Order_Description = (TextView) v
.findViewById(R.id.description);
holder.txt_Date = (TextView) v
.findViewById(R.id.collectionDate);
holder.orderCheckBox = (CheckBox) v
.findViewById(R.id.checkboxorder);
holder.txt_TrackingNumber = (TextView) v
.findViewById(R.id.accession);
if(checkboxstate[position]==null){
checkboxstate[position]=false;
}
if(observationDetailsList.get(position).GenerateLabOrder.equalsIgnoreCase("Flase"))
{
holder.orderCheckBox.setChecked(checkboxstate[position]);
}
if (observationDetailsList.get(position).GenerateLabOrder
.equalsIgnoreCase("False"))
{
holder.orderCheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked()){
if (checkboxstate[position] = true)
{
patientOrderList.add(orderDetailsList.get(position));
patientObservationList.add(observationDetailsList.get(position));
}
else{
if (checkboxstate[position] = false)
{
checkboxstate[position]=false;
patientOrderList.remove(orderDetailsList.get(position));
patientObservationList.remove(observationDetailsList.get(position));
}
}
}
}
});
}
v.setTag(holder);
}
return v;
}
}