我看到了这个问题android how to hide linearlayout when no checkbox is check in listview 我在每一行都有带有复选框的列表视图,我希望当至少有一个复选框被选中时,一个线性布局(包含两个按钮)可见并且没有复选框被选中我希望该 lineylayout 隐藏,
我提出了完全一样的问题,但它只适用于第一个复选框,我的意思是当我选中第一个复选框时它工作,然后如果我取消选中线性布局隐藏但如果我选中另一个复选框,线性布局仍然始终可见适配器
public class AdapterFavoriteFoods extends BaseAdapter {
private ArrayList<Boolean> cb_status = new ArrayList<Boolean>();
private static LayoutInflater inflater = null;
private ArrayList<HashMap<String, String>> data;
Activity activity;
LinearLayout ll_CancelDone;
private int checkedBoxs = 0;
CheckBox[] allCheckBoxes;
public AdapterFavoriteFoods(Activity activity,
ArrayList<HashMap<String, String>> data, LinearLayout ll_CancelDone) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.data = data;
this.ll_CancelDone = ll_CancelDone;
inflater = (LayoutInflater) this.activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < data.size(); i++) {
cb_status.add(false);
}
allCheckBoxes = new CheckBox[data.size()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.profile_list_item_checkbox, null);
TextView tv_name = (TextView) vi
.findViewById(R.id.tv_profile_list_item_checkbox_title);
ImageView iv_image = (ImageView) vi
.findViewById(R.id.iv_profile_list_item_checkbos_image);
CheckBox cb_selected = (CheckBox) vi
.findViewById(R.id.cb_profile_list_item_checkbox_checkbox);
allCheckBoxes[position] = cb_selected;
cb_selected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
cb_status.set(position, true);
checkedBoxs++;
if (ll_CancelDone.getVisibility() == View.GONE)
ll_CancelDone.setVisibility(View.VISIBLE);
} else {
cb_status.set(position, false);
checkedBoxs--;
if (checkedBoxs == 0)
ll_CancelDone.setVisibility(View.GONE);
}
}
});
cb_selected.setChecked(cb_status.get(position));
HashMap<String, String> hm = data.get(position);
tv_name.setText(hm.get("name"));
iv_image.setImageResource(Integer.parseInt(hm.get("image")));
return vi;
}
}
[1]: https://stackoverflow.com/questions/14469590/android-how-to-hide-linearlayout-when-no-checkbox-is-checked-in-listview
当我Log.d("num", checkedBoxs+"");
在 oncheckchange 的 else 语句中执行此操作时,结果仍然为 1 ,