Android checkbox.isChecked()无法正常工作 [英] Android checkbox.isChecked() does not work properly
本文介绍了Android checkbox.isChecked()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ListView
,其中每行显示一个复选框
。
每当触摸复选框
时,我会检查是否选中。
但每次,第一个项目总是返回 false
。但是,如果检查第二项,则第一项复选框的 .ischecked()
方法返回 true
。
这是我的代码:
I have a ListView
in which a checkbox
is displayed in each row.
Each and every time the checkbox
is touched, I check wheteher it is checked or not.
But every time, the first item always return false
. However, If if checked the 2nd item, the .ischecked()
method of the checkbox of the first item alway return true
.
Here is my code:
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;
}
}
问题?
提前非常感谢!
What should I do to solve this issue? Many thanks in advance!
推荐答案
您应该使用setOnCheckedChangeListener。
You should use setOnCheckedChangeListener.
示例:
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
}
});
在您的代码中:
cbSelectedCar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked){
caller.updateClickedUsername(currentJson, true); // Add to the List
Log.d("Added click ", "ok");
}
else if (!isChecked) {
caller.updateClickedUsername(currentJson, false); // Delete from the List
Log.d("Deleted click ", "nok");
}
}
});
这篇关于Android checkbox.isChecked()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文