取消选中自定义ListView的第一个复选框时,LinearLayout会隐藏一次

时间:2013-01-25 15:43:03

标签: android android-layout android-listview

我看到了这个问题android how to hide linearlayout when no checkbox is checked in listview 我有每行中带复选框的列表视图,我想当至少有一个复选框被选中时,linearylayout(包含两个按钮)是可见的,当没有选中复选框时我想让lineylayout隐藏,

我完全喜欢这个问题,但它只适用于第一个复选框,我的意思是当我检查它工作的第一个检查框时,如果我取消选中linearlayout hide但是如果我检查另一个复选框,则linearlayout仍然可见 适配器

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

当我在oncheckchange的else statemnt中执行此Log.d("num", checkedBoxs+"");时,结果仍为1,

1 个答案:

答案 0 :(得分:1)

这不是你应该如何保持状态,最重要的是不要保留对行CheckBoxes的引用。试试这个:

//...
CheckBox cb_selected = (CheckBox) vi.findViewById(R.id.cb_profile_list_item_checkbox_checkbox);
cb_selected.setChecked(cb_status.get(position));
cb_selected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
          cb_status.set(position, isChecked);
          boolean status = false;
          for (Boolean b : cb_status) {
               if (b) {
                   status = true;
                   break;   
               }
          }  
          ll_CancelDone.setVisibility(status ? View.VISIBLE : View.GONE);
     }
});