Disabling CheckBox in MultiChoiceItems AlertDialog

Issue

I have an AlertDialog which it has dynamic items linked to a Cursor from database , it works fine but i wanted to disable user interaction with that because it’s an informational dialog , i’ve disabled my dialog with :

alert.getListView().setEnabled(false);

but the problem is when Items in the list are bigger than the dialog height , because of disabling it’s ListView, Scrolling is disabled too and some items doesn’t show up, i’ve tried some links like : Android : How to disable CheckBox in AlertDialog? with no success. i’ve also tried :

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

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

                            if(isCheckedtrue)
                            {

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


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

Does anybody know a better way to disable the checkboxes without a Custom dialog? Thanks in Advance.

Solution

this may give you some idea…

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;
    }

}

Answered By – Gopal Gopi

Leave a Comment