【问题标题】:Set different default checked/unchecked on AlertDialog在 AlertDialog 上设置不同的默认选中/取消选中
【发布时间】:2015-03-12 16:08:28
【问题描述】:

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

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

那么,

我想创建一个复选框alertDialog,这个AlertDialog会显示来自ListGroupId的一些数据,然后我想比较ListGroupId和groupIdBefore,如果ListGroupId的值与groupIdBefore相等,

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

这是我的代码,

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

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

你能帮我解决这个问题吗?我真的不明白复选框警报对话框是如何工作的

提前谢谢你

【问题讨论】:

    标签: java android checkbox android-alertdialog


    【解决方案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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 2022-01-09
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多