2011-08-21 65 views
4

我想通过指定一个字符串值来查找GWT列表框内的项目的索引。通过字符串在GWT列表框中查找

例如,如果我有一个包含以下项目的GWT列表框:“Randy”,“Bob”和“Helen”,我希望实现的方法将返回值1,如果我用参数"Bob"

从我在ListBox javadoc中看到的,似乎没有任何快速的方法来做到这一点。

任何想法?

在此先感谢!

+0

见这一项指标.. http://stackoverflow.com/questions/6986793/gwt-listbox-how-to-look-up -item-index-using-text –

回答

1

我作为TextBox实现的想法并没有提供这种开箱即用的功能。将所有项目存储在列表中,并按您希望它们成为ListBox的一部分的顺序存储。

List<String> orderedItems=new ArrayList<String> 

orderedItems.add(0,"Randy"); 
orderedItems.add(1,"Bob"); 
orderedItems.add(2,"Helen"); 

//adding items in the same order as they are in List is the key 
for(String item:items) 
{ 
    lb.addItem(item); 
} 

然后你可以找到使用List's indexOf(..) method

+0

添加一个全新的数据结构只是做这不是一个好主意。 –

相关问题