如何从列表中获取所有选中的项?
我需要从列表中获取所有选中(选中)的项,并填充一个向量。
我没有获得所有选定的项目,我只获得当前焦点所在的项目。
根据知识库文章,我正在使用复选框实现listfield。
如果我使用getSelection(),它将返回当前突出显示的列表行索引,而不是所有已检查的列表行索引。
发布于 2009-07-27 05:56:56
据我所知,样本是How To - Create a ListField with check boxes
然后,您可以将向量添加到实现ListFieldCallback的类:
private Vector _checkedData = new Vector();
public Vector getCheckedItems() {
return _checkedData;
}并以这种方式更新drawListRow:
if (currentRow.isChecked())
{
if( -1 ==_checkedData.indexOf(currentRow))
_checkedData.addElement(currentRow);
rowString.append(Characters.BALLOT_BOX_WITH_CHECK);
}
else
{
if( -1 !=_checkedData.indexOf(currentRow))
_checkedData.removeElement(currentRow);
rowString.append(Characters.BALLOT_BOX);
}如果你想在自定义CheckBoxField中使用VerticalFieldManager,你可以遍历屏幕上的所有字段(或者任何管理器),检查它的复选框字段,然后取一个值:
class List extends VerticalFieldManager {
...
public Vector getCheckedItems() {
Vector result = new Vector();
for (int i = 0, cnt = getFieldCount(); i < cnt; i++) {
Field field = getField(i);
if (field instanceof CheckboxField) {
CheckboxField checkboxField = (CheckboxField) field;
if (checkboxField.isChecked())
result.addElement(checkboxField);
}
}
return result;
}
}发布于 2012-05-02 12:51:14
@sandhya-m
class List extends VerticalFieldManager {
...
public void selectAll() {
for (int i = 0, cnt = getFieldCount(); i < cnt; i++) {
Field field = getField(i);
if (field instanceof CheckboxField) {
CheckboxField checkboxField = (CheckboxField) field;
checkboxField.setChecked(true);
}
}
}
}https://stackoverflow.com/questions/1186383
复制相似问题