【问题标题】:Blackberry - get checked items from list with checkboxes黑莓 - 从带有复选框的列表中获取选中的项目
【发布时间】:2009-07-27 04:07:35
【问题描述】:

如何从列表中获取所有选中的项目?

我需要从列表中获取所有选定(选中)的项目并填充一个向量。

我没有得到所有选定的项目,我只得到当前焦点所在的项目。

我正在根据知识库文章实现带有复选框的列表字段。

如果我使用 getSelection(),它会返回当前突出显示的列表行索引,而不是所有已检查的索引。

【问题讨论】:

    标签: user-interface blackberry java-me listfield


    【解决方案1】:

    据我所知,样本是How To - Create a ListField with check boxes

    然后可以在实现ListFieldCallback的类中添加Vector:

    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);
    }
    

    如果您将 VerticalFieldManager 与自定义 CheckBoxField 一起使用,您可以遍历屏幕上的所有字段(或任何管理器)并检查其复选框字段是否,然后取一个值:

    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;
        }
    }
    

    【讨论】:

    • 如何在单击按钮 selectall 时一次选择列表字段中的所有项目。如果有人对 htat 有想法,请给您建议。
    【解决方案2】:

    @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);
                    }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多