-2

我需要您的帮助才能将列表的值转换为两个变量。我的清单有描述和代码。但是,我需要将描述放在一个变量中,将代码放在一个不同的变量中,那么我该如何实现这一点。

我的代码是

private String[] selectedCertificates;
private List<SelectItem> Certificates;

    public List<SelectItem> getCertificatesList(){
    Certificates = new ArrayList<SelectItem>();
    Certificates.add(new SelectItem("Certificate A","A"));
    Certificates.add(new SelectItem("Certificate B","B"));
    return bankCertificates;

}

public void setCertificates(List<SelectItem> Certificates) {
    this.Certificates = Certificates;
}
// Setters and Getters

选择项目代码:

                         <p:selectManyCheckbox id="Certificates" value="#{user.selectedCertificates}"
                                              layout="pageDirection" disabled="#{user.secondToggle}">
                            <f:selectItems value="#{user.Certificates}" var="bankCertificates"
                                           itemLabel="#{user.CertificatesString}" itemValue="#{user.CertificatesCode}"/>
                        </p:selectManyCheckbox>

我在哪里可以定义描述应该是第一个值,代码应该是列表中的第二个值,我可以在页面中使用它们。

谢谢

4

2 回答 2

0

尝试

class SelectItem {
    private String code;
    private String description;

    SelectItem (String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode () {
        return code;
    }
    public String getDescription () {
        return description;
    }
}

这是你的主要课程

class MainClass {
    public static void main (String...arg) {
        //construct your list here using SelectItem class objects
        List<SelectItem> certificates =  = new ArrayList<SelectItem>();
        certificates.add(new SelectItem("Certificate A","A"));
        certificates.add(new SelectItem("Certificate B","B"));

        //now first read the SelectItem objects you have added to the list
        //or you can also iterate through the list, modify accordingly
        SelectItem si1 = certificates.get(0);
        //to read the code and description use the getters defined in SelectItem
        si1.getCode(); si1.getDescription();
    }
}

您还可以选择创建一个方法,您可以将希望从列表中读取的索引传递给该方法。希望这可以帮助。

于 2015-03-06T08:43:12.587 回答
0

如果您的 SelectItem bean 上有 getter(我假设您有字段描述和代码),请尝试按照以下方式进行操作,它存储您的对象字段,该字段位于您的 ArrayList 中的第一个位置。

String description = Certificates.get(0).getDescription();
String code = Certificates.get(0).getCode();
于 2015-03-06T08:22:30.640 回答