在MultiChoiceItems AlertDialog中禁用CheckBox

时间:2013-11-06 08:45:50

标签: android checkbox dialog android-alertdialog multichoiceitems

我有一个AlertDialog,它有动态项链接到数据库中的Cursor,它工作正常,但我想禁用用户交互,因为它是一个信息对话框,我已禁用我的对话框:< / p>

alert.getListView().setEnabled(false);

但问题是当列表中的项目大于对话框高度时,由于禁用它的ListView,滚动也被禁用而某些项目没有显示,我尝试了一些链接,如:{{3} } 没有成功。我也试过了:

builder.setMultiChoiceItems(mDBAdapter.instance.getName(SupervisorGuid)
                , SyncDBHelper.instance.SentSupervisors(SupervisorGuid),new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {

                            if(isChecked==true)
                            {

                                    ((AlertDialog) dialog).getListView().setItemChecked(which, false);
                            }
                            else
                            {
                                    ((AlertDialog) dialog).getListView().setItemChecked(which, true); 
                            }


                    }});
        AlertDialog alert = builder.create();

有没有人知道在没有自定义对话框的情况下禁用复选框的更好方法? 在此先感谢。

2 个答案:

答案 0 :(得分:2)

这可能会给你一些想法...

private void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    ArrayList<String> arrayList = new ArrayList<String>();
    ArrayList<Boolean> selectedList = new ArrayList<Boolean>();
    for (int i = 1; i <= 20; i++) {
        arrayList.add("" + i);
        selectedList.add(i % 2 == 0);
    }
    builder.setAdapter(new MyAdapter(arrayList, selectedList), null);
    builder.show();
}


 public static class MyAdapter extends BaseAdapter {

    private ArrayList<String> arrayList;
    private ArrayList<Boolean> selectedList;

    public MyAdapter(ArrayList<String> arrayList,
            ArrayList<Boolean> selectedList) {
        this.arrayList = arrayList;
        this.selectedList = selectedList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getView(parent.getContext(), position);
    }

    private RelativeLayout getView(Context context, int position) {
        RelativeLayout relativeLayout = new RelativeLayout(context);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        relativeLayout.setLayoutParams(params);
        TextView textView = new TextView(context);
        textView.setText(arrayList.get(position));
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        layoutParams.leftMargin = 30;
        textView.setLayoutParams(layoutParams);
        relativeLayout.addView(textView);
        CheckBox checkBox = new CheckBox(context);
        layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.rightMargin = 30;
        checkBox.setLayoutParams(layoutParams);
        checkBox.setClickable(false);
        if (selectedList != null) {
            checkBox.setChecked(selectedList.get(position));
        }
        relativeLayout.addView(checkBox);
        return relativeLayout;
    }

}

答案 1 :(得分:0)

我遇到了同样的问题并以这种方式解决了这个问题:

AlertDialog.Builder builder = new AlertDialog.Builder(this);

...

builder
    .setTitle(...)
    .setMultiChoiceItems(cursor, IS_CHECKED, LABEL, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                ((AlertDialog)dialog).getListView().setItemChecked(which, /* assign here the original boolean value of the item */);
            }
    })

...

AlertDialog dialog = builder.create();
dialog.show();

总之,当您单击列表中的项目时,只需将其值设置回原始布尔值。