문제

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(isChecked==true)
                            {

                                    ((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.

도움이 되었습니까?

해결책

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

}

다른 팁

I had the same problem and solved it this way:

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();

In summary, when you click the item in the list, just set its value back to the original boolean value.

you can define your check box value with boolean array "checkValues" is our boolean array which defines which check box should tick or which not. check below ex.

builder.setMultiChoiceItems(dialogList, checkedValues)

    private fun showDialog() {
    val dialogList: Array<String> = days
    val checkedValues = BooleanArray(days.size)
    checkedValues[viewpager.currentItem] = true
    val builder: AlertDialog.Builder = AlertDialog.Builder(activity)
    

    builder.setTitle("You can copy"+dialogList[viewpager.currentItem]+" time slot to:")
    builder.setMultiChoiceItems(dialogList, checkedValues)
    { dialog, which, isChecked ->
        (dialog as AlertDialog).listView.setItemChecked(yourIndex, true)

    }.setPositiveButton("OK") { dialog, id ->
      
            dialog.dismiss()
    }.setNegativeButton("Cancel") { dialog, id ->
            dialog.dismiss()
    }.show()
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top