Java Android checkbox.isChecked()无法正常工作
我有一个Java Android checkbox.isChecked()无法正常工作,java,android,checkbox,Java,Android,Checkbox,我有一个列表视图,其中每行显示一个复选框 每次触摸复选框,我都会检查它是否被选中 但每次,第一项总是返回false。但是,如果选中第二项,则第一项复选框的.ischecked()方法始终返回true 这是我的代码: public class CustomSpecificCar extends ArrayAdapter<JSONObject>{ ListSpecificCars caller; private JSONObject currentJson; pr
列表视图
,其中每行显示一个复选框
每次触摸
复选框
,我都会检查它是否被选中但每次,第一项总是返回
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;
}
}
公共类CustomSpecificCar扩展ArrayAdapter{
电话号码;
私有JSONObject currentJson;
私家车;
私人职位;
公共CustomSpecificCar(上下文ctxt、列表列表列表、列表SpecificCars调用者){
超级(ctxt,R.layout.custom\u specific\u car\u row,list);
this.caller=caller;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=LayoutInflater.from(getContext());
视图customView=充气机。充气(R.layout.custom\u specific\u car\u row,父项,false);
这个位置=位置;
//设置布局的参照
currentJson=getItem(this.position);
cbSelectedCar=(复选框)customView.findviewbyd(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);
试一试{
tvBrand.setText(currentJson.getString(“brand”);
setText(currentJson.getString(“model”);
tvOwnerEditable.setText(currentJson.getString(“所有者”);
}捕获(JSONException e){
e(e.getClass().getName(),“JSONException”,e);
}
cbSelectedCar.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(cbSelectedCar.isChecked()){
updateClickedUsername(currentJson,true);//添加到列表中
Log.d(“添加单击”、“确定”);
}
如果(!cbSelectedCar.isChecked()){
updateClickedUsername(currentJson,false);//从列表中删除
Log.d(“删除的点击”、“不确定”);
}
}});
返回自定义视图;
}
}
我应该如何解决这个问题?
非常感谢 您应该使用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");
}
}
});
对于使用复选框作为Onclicklistener,这将帮助您
CheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(this, "CheckBox is checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "CheckBox is not checked", Toast.LENGTH_SHORT).show();
}
});
欢迎光临。别忘了把答案投上一票,这样别人就可以把这看作是一个“解决方案”了!