2

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 selected items, I am getting only the item on which current focus is.

I am implementing listfield with checkboxes as per the knowledgebase article.

If I use getSelection(), it is returning me the currently highlighted list row index, and not all that have been checked.

gnat
  • 6,213
  • 108
  • 53
  • 73
iOSDev
  • 3,617
  • 10
  • 51
  • 91

2 Answers2

3

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;
    }
}
kehers
  • 4,076
  • 3
  • 30
  • 31
Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
  • how to select all items at a time in a listfield when clicking on the button selectall.plz give ur suggestions if anyone have idea about htat. – user1213202 Mar 06 '12 at 06:03
0

@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);
                }
        }
    }
}
kehers
  • 4,076
  • 3
  • 30
  • 31