我有一个selectItem值和标签列表.数据从数据库中获取,selectItem列表具有以下值:
<1,100-500>
<2,1000-1500>
<3,500-1000>
Run Code Online (Sandbox Code Playgroud)
这里1,2,3是selectItem列表的值,'100-500','1000-1500'和'500-1000'分别是标签.如您所见,列表已根据标签进行排序.但我的要求是列表应显示在下拉列表中,如下所示:
100-500
500-1000
1000-1500
Run Code Online (Sandbox Code Playgroud)
有人可以建议一个解决方案?
如果您无法修改从DB中获取SelectItem实例的代码,以便按照您的喜好进行排序,那么您必须自己对它们进行排序:
// this gets you a list which is not sorted as you would like
List<SelectItem> list = getMasterValues("Staff Range");
// you need to sort the list before displaying it.
// To sort the list, you need a Comparator which will tell the sorting
// algorithm how to order the items
Comparator<SelectItem> comparator = new Comparator<SelectItem>() {
@Override
public int compare(SelectItem s1, SelectItem s2) {
// the items must be compared based on their value (assuming String or Integer value here)
return s1.getValue().compareTo(s2.getValue());
}
};
// now that you have the comparator, sort the list using it :
Collections.sort(list, comparator);
// and now iterate over the list to display it :
for (SelectItem item : list) {
System.out.println("value = " + item.getValue() + "; label = " + item.getLabel());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15080 次 |
| 最近记录: |