在AlertDialog上设置不同的默认选中/未选中

时间:2015-01-14 12:59:43

标签: java android checkbox alertdialog

我有一个程序会将HashMap(比如groupIdBefore)传递给一个新的Activity,

在此活动中,我将从数据库中获取一些数据并将其存储在HashMap(ListGroupId)中

然后,

我想创建一个复选框alertDialog,这个AlertDialog将显示ListGroupId中的一些数据,然后我想比较ListGroupId和groupIdBefore,如果ListGroupId的值等于groupIdBefore,

然后(当显示复选框AlertDialog时)复选框设置为Checked,否则复选框保持未选中状态。

这是我的代码,

public void alertDialogBuilder() {
    // TODO Auto-generated method stub
    Log.d("Flag", "arrive at alertdialogBuilder");
    final String[] listGroupName = new String[mGroupList.size()];
    final String[] listGroupId = new String[mGroupList.size()];
    final boolean[] itemsChecked = new boolean[mGroupList.size()];

    final String[] groupIdBefore = new String[localGroupList.size()];

    // Get hashmap from previous Activity and convert to string
    for (int i = 0; i < localGroupList.size(); i++) {
        groupIdBefore[i] = localGroupList.get(i).get("groupId");
    }

    // Convert all groupId and groupName produced by this activity (all
    // groups this User has)
    for (int i = 0; i < mGroupList.size(); i++) {
        Dat[i] = mGroupList.get(i).get("groupName");
        listGroupId[i] = mGroupList.get(i).get("groupId");

        for (int j = 0; j < localGroupList.size(); j++) {
            if (listGroupId[i].equals(groupIdBefore[j])) {
                itemsChecked[i] = true;
            }
        }

    }

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

    builder.setTitle("Choose Your Group : ");

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            groupList.clear();
            stringList.clear();
            selectedGroups.setText("Post To : ");

            for (int i = 0; i < listGroupName.length; i++) {
                if (itemsChecked[i]) {
                    groupList.add(listGroupName[i]);
                    stringList.add(listGroupId[i]);
                    itemsChecked[i] = false;
                }
                Log.d("flag", "after for");

            }
            String string = "";
            for (int i = 0; i < groupList.size(); i++) {
                selectedGroups.setText("Post To : ");
                string = string + " " + groupList.get(i);

            }
            selectedGroups.setText(selectedGroups.getText().toString()
                    + string);
        }
    });
    builder.setMultiChoiceItems(listGroupName, new boolean[] { false,
            false, false },
            new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which,
                        boolean isChecked) {
                    itemsChecked[which] = isChecked;
                }
            });
    builder.show();
    Log.d("flag", "bottom of alert dialog");
}

但不检查具有相同值的项目,

你能帮我解决这个问题吗?我真的不明白checkbox alertdialog是如何工作的

提前谢谢

1 个答案:

答案 0 :(得分:1)

我修改了我的代码,在这里我更改了代码:

builder.setMultiChoiceItems(listGroupName, itemsChecked,
            new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which,
                        boolean isChecked) {
                    itemsChecked[which] = isChecked;
                }
            });
    builder.show();
    Log.d("flag", "bottom of alert dialog");
}