Blackberry - get checked items from list with checkboxes

后端 未结 2 2012
栀梦
栀梦 2021-01-07 05:11

How can all checked items from a list can be fetched?

I need to get all selected (checked) items from the list and populate a vector.

I am not getting all s

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 05:40

    As I undestood, sample is How To - Create a ListField with check boxes

    Then you can add Vector to the class where ListFieldCallback is implemented:

    private Vector _checkedData = new Vector();
    public Vector getCheckedItems() {
            return _checkedData;
        }
    

    and update drawListRow this way:

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

    If you would use VerticalFieldManager with custom CheckBoxField, you could iterate over all fields on screen (or any manager) and check if its' checkbox field, then take a value:

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

提交回复
热议问题