How to get a specific attribute from an object list?

Posted by anarhikos on Stack Overflow See other posts from Stack Overflow or by anarhikos
Published on 2010-10-31T02:04:46Z Indexed on 2013/07/02 23:06 UTC
Read the original article Hit count: 146

Filed under:
|

I've an array keeping a list of Group objects. I want to set this list to the DropDownChoice component. However I want to show the end user only the name attribute of Group objects, and then get the selected values' id attribute to add database. What to do?

private List<Group> groupTypes;
DatabaseApp db = new DatabaseApp();
groupTypes = db.getGroups();
groupDropDownChoice = new DropDownChoice("type", groupTypes);
...
...
addUserForm.add(new Button("submit"){
            @Override
            public void onSubmit(){
                Group group = (Group) groupDropDownChoice.getModelObject(); 
...
...
            db.addUser(group.getId(), den, name, login, email, password1);

DatabaseApp.java

public List<Group> getGroups() throws SQLException{
        List<Group> groups = new ArrayList<Group>();

        try {
            String query = "SELECT * FROM [GROUP]";
            Statement statement = db.createStatement();
            ResultSet result = statement.executeQuery(query);

            while(result.next()){
                int id = result.getInt("ID");
                String name = result.getString("NAME");
                groups.add(new Group(id, name));
            }
            result.close();

        } catch (SQLException ex) {
            throw new SQLException(ex.getMessage());
        }
            return groups;
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about wicket