-2

I need your help in getting the values of a list into two variables. My list is having descriptions and codes. However, I need to place the descriptions in a variable and the codes in a different variable, so how can I achieve this.

My Code is

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

Select Item Code:

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

where can I define that the description should be the first value and the code should be the second value in the list and I can use them in the page.

Thanks

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
maas maas
  • 19
  • 1
  • 5

2 Answers2

0

Try to do as folows if you have getters on your SelectItem bean (I have supposed you have fields description and code), it stores your object fields which is at first position in your ArrayList.

String description = Certificates.get(0).getDescription();
String code = Certificates.get(0).getCode();
drgPP
  • 926
  • 2
  • 7
  • 22
  • Will it get one value only or in a list? Can I do it in the same bean? – maas maas Mar 06 '15 at 08:30
  • It will get the values of your object fields and store them into your description and code variables, you do this where you need, if you want you could declare your variables in your page and initializing them with your object fields which is contained in the list, or you can initialize them in your controller(or whatever you have) and pass to the page only the description and code variables (but you need to know exaclty which object you need from your list, on which index it is located). – drgPP Mar 06 '15 at 09:01
0

Try

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

Here is your main class

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

You can also choose to create a method to which you can pass the index which you wish to read from the list. Hope, this helps.

Tirath
  • 2,294
  • 18
  • 27