如何制作在 GWT 中检查的对象列表?

How to make a list of objects which are checked in GWT?

我正在开发一个使用复选框的 GWT 应用程序。我有复选框中的 GwtRoles 列表,但我不知道如何获取那些已选中的 GwtRoles。这是我的代码:

@Override
    public void createBody() {

    for (GwtRole gwtRole : roleList) {
                checkBox = new CheckBox();
                checkBox.setBoxLabel(gwtRole.getName());
                for (GwtAccessRole gwtAccessRole : lista) {
                    if (gwtRole.getId().equals(gwtAccessRole.getRoleId())) {
                        checkBox.setValue(true);
                    }

RoleList 是复选框中的 GwtRoles 列表。此列表是用户打开表单时应预先检查的项目列表。我不太熟悉 GWT 中的复选框。 我使用了 CheckBox 列表,那里有方法 getChecked(),其中 returns 列出了所有已选中的 GwtRoles,但这里使用此复选框我没有该选项。 在这种方法中,我应该制作一个已检查的 GwtRoles 列表:

 @Override
    public void submit() {

        List<GwtAccessRoleCreator> listCreator = new ArrayList<GwtAccessRoleCreator>();

        for (GwtRole role : list) {
            GwtAccessRoleCreator gwtAccessRoleCreator = new GwtAccessRoleCreator();

            gwtAccessRoleCreator.setScopeId(currentSession.getSelectedAccount().getId());

            gwtAccessRoleCreator.setAccessInfoId(accessInfoId);

            gwtAccessRoleCreator.setRoleId(role.getId());
            listCreator.add(gwtAccessRoleCreator);
        }
        GWT_ACCESS_ROLE_SERVICE.createCheck(xsrfToken, currentSession.getSelectedAccount().getId(), userId, listCreator, new AsyncCallback<GwtAccessRole>() {

            @Override
            public void onSuccess(GwtAccessRole arg0) {
                exitStatus = true;
                exitMessage = MSGS.dialogAddConfirmation();
                hide();
            }

有人可以帮我设置一个 GwtRoles 列表吗?

Map 中跟踪您的 CheckBox,然后仅 return 选中复选框的 GwtRole

private Map<GwtRole, CheckBox> mapping = new HashMap<>();

@Override
public void createBody() {
    for (GwtRole gwtRole : roleList) {
        CheckBox checkBox = new CheckBox();
        mapping.put(gwtRole, checkBox);
        // Your other code here.
    }
}

// Java 8
public List<GwtRole> getChecked()
{
    return mapping.entrySet().stream()
        .filter(e -> e.getValue().getValue())
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());
}

// Pre-Java 8
public List<GwtRole> getChecked()
{
    List<GwtRole> result = new ArrayList<>();

    for(Map.Entry<GwtRole, CheckBox> e : map.entrySet())
    {
        if(e.getValue().getValue())
            result.add(e.getKey());
    }

    return result;
}