我有一个 ListView
,每一行都显示一个checkbox
。每次点击checkbox
时,我都会检查它是否被选中。但是每次第一项始终返回false
。然而,如果选中了第二项,则第一项的.ischecked()
方法总是返回true
。
以下是我的代码:
public class CustomSpecificCar extends ArrayAdapter<JSONObject>{
ListSpecificCars caller;
private JSONObject currentJson;
private CheckBox cbSelectedCar;
private int position;
public CustomSpecificCar(Context ctxt, List<JSONObject> list, ListSpecificCars caller){
super(ctxt, R.layout.custom_specific_car_row, list);
this.caller = caller;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_specific_car_row, parent, false);
this.position = position;
// Set the reference of the layout
currentJson = getItem(this.position);
cbSelectedCar = (CheckBox)customView.findViewById(R.id.cbSelectedCar);
TextView tvBrand = (TextView)customView.findViewById(R.id.tvBrand);
TextView tvModel = (TextView)customView.findViewById(R.id.tvModel);
TextView tvOwnerEditable = (TextView)customView.findViewById(R.id.tvOwnerEditable);
TextView tvPriceEditable = (TextView)customView.findViewById(R.id.tvEstimatedPriceEditable);
try {
tvBrand.setText(currentJson.getString("brand"));
tvModel.setText(currentJson.getString("model"));
tvOwnerEditable.setText(currentJson.getString("owner"));
} catch (JSONException e) {
Log.e(e.getClass().getName(), "JSONException", e);
}
cbSelectedCar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (cbSelectedCar.isChecked()){
caller.updateClickedUsername(currentJson, true); // Add to the List
Log.d("Added click ", "ok");
}
else if (!cbSelectedCar.isChecked()) {
caller.updateClickedUsername(currentJson, false); // Delete from the List
Log.d("Deleted click ", "nok");
}
}});
return customView;
}
}
我该怎么做来解决这个问题?非常感谢!