如何将ArrayList添加到GWT ListBox

时间:2014-02-14 05:54:52

标签: java gwt arraylist listbox

我有一个arrayList:

final List<User> users = new ArrayList<User>();

我想将此arrayList的结果users添加到GWT ListBox。

ListBox就像:

 private ListBox test;
 test = new ListBox();
 // Here i want to add the users as additem

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码:

class CustomListBox extends ListBox {
    public void addUsersList(List<User> userList) {
        for (User user : userList) {
            addItem(user.toString());
    }
}

CustomListBox customListBox = new CustomListBox();
customListBox.addUsersList(getUsersList());
customListBox.setValue(getUsersList().get(1));

您还可以使用ValueListBox组件:

ValueListBox<Users> customListBox = new ValueListBox<Users>(new Renderer<Users>() {
    public String render(Users users) {
        String s = "Select a user";
        if(users != null) {
            StringBuilder sb = new StringBuilder();
            sb.append(users.getUsername());
            sb.append("(");
            sb.append(users.getFirstname());
            sb.append(" ");
            sb.append(users.getLastname());
            sb.append(")").toString();

            s = sb.toString();
        }
        return s;
    }

    public void render(Users users, Appendable appendable) throws IOException {
        String s = render(users);
        appendable.append(s);
  }
});

customListBox.setValue(getUsersList().get(1));
customListBox.setAcceptableValues(getUsersList());

希望这有帮助。