Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530
Accepted Solution

Any solution to add Checkbox[] without

 

Hi,

 

    I want to add number of checkbox (added dynamically) with various info next to it. I am able to add it with all info the way I want it. But am not able to figure out how to find which checkbox was clicked.

 

   In a vfm, I add a gridFieldManager which has checkbox[i], and 3 labels. I add setChangeListener to each checkbox[i], when any check box is clicked, I am not able to know which index of chexkbox was clicked. in fieldChanged(), I gave SOP "Field = " + field & it sows "Field = [YEs]".

 

   Any clue how to find out which index of checkbox was clicked from an [] of checkboxes ? I had a look at 

How To - Create a ListField with check boxes

 

but in that I will have to passa single string as an element. And in my case I have to add 3 labels with specified sizes, which gives each column proepr size and layout.

 

   Any help, guidance is highly appreciated.

 

Thanks

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Any solution to add Checkbox[] without

in fieldChanged you can

- check which field was changed by using if (field==checkbox1)...

- check the label of the field by casting field to checkboxfield and calling getLabel

- do the same for a cookie you set before

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 45
Registered: ‎05-25-2010
My Device: 8300
My Carrier: developer

Re: Any solution to add Checkbox[] without

Variants:

1) Create custom CheckBox, with additional attribute - index, or dataobject. In listener cast to this object and get attribute;

2) Field.getIndex(): Retrieves position of this field within its manager.

Contributor
Posts: 44
Registered: ‎06-07-2010
My Device: 9630
My Carrier: Verizon

Re: Any solution to add Checkbox[] without

Quick-and-dirty approach would be to set the cookie with information you need to identify the field.  That way, you can retrieve the cookie information to find out which field was involved.  Other suggestions already mentioned are probably better in the long term.

Developer
Posts: 1,635
Registered: ‎07-14-2008
My Device: Z10
My Carrier: Fido

Re: Any solution to add Checkbox[] without

I agreed.  Just make sure you cast to checkboxField.

You can either compare the instance or check the checkbox label.

 

Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Any solution to add Checkbox[] without

 

Thanks to All Friends,

    I don't assign value to label of the Checkbox. If at all, I add then also the label value may be same for more than 1 checkbox; so that's not reliable. And as it's an array so can't check with

field == checkbox1, as checkbox1 obj is never created. It will be always checkbox[i].

   You all have mentioned using cookie, can you tell how to use it. I have no idea of using cookie in such a way.

   Pasha, your both options seems appropriate. Your 2nd option i.e. Field.getIndex(), if I have 20 checkbox[], they will be scrolling, then will I get the right answer. I mean initally at certain x, y their may be checkbox[5], then after scrolling at the same position their may be checbox[10]. Will I get right index for the right field ?  I didn't get chance to check out but will check out.

   Kindly help me know about how to use cookie in this issue.

Thanks

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Posts: 1,635
Registered: ‎07-14-2008
My Device: Z10
My Carrier: Fido

Re: Any solution to add Checkbox[] without

since your checkboxes are in array of CheckBox you can just set all the checkboxes to one listener.

 

in the listener do this

 

if (field instanceof checkbox[0])

{

}

 

or you check compare it thru look of all checkboxes

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Any solution to add Checkbox[] without

Re: using cookies:

 

When you create your checkbox, you can issue checkbox.setCookie(anyObject). I assume you also add the same FieldChangeListener to all such checkboxes.  Different checkboxes should get different cookies, of course.

 

In your fieldChanged(Field field, int context) you can retrieve the cookie using field.getCookie(). You can then also cast the field to CheckboxField and retrieve its checked status. If you plan it correctly, your cookie and this status together should be enough for you to proceed.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
New Developer
Posts: 45
Registered: ‎05-25-2010
My Device: 8300
My Carrier: developer

Re: Any solution to add Checkbox[] without

Field.getIndex() is not changed during scroll.

From JavaDoc: Retrieves position of this field within its manager.

 

If manager contains only CheckBoxes, index will be worked correctly.

Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Any solution to add Checkbox[] without

 

THANKS arkadyz,

 

    This solution helped me for my task. I was accessing an obj from Vector and displaying data in a row using Checkbox. Added that obj as a Cookie of the checkbox & retrieved the obj in fieldChanged.

 

  MyObject mo = vector[i];

  checks[i] = new CheckboxField();
  checks[i].setCookie(mo);
  checks[i].setChangeListener(this);

  ffm.add(checks[i]);

  ffm.add(mo.name);

  ffm.add(mo.refName);

  add(ffm);

 

// In fieldChanged()

if (f instanceof CheckboxField) {
            CheckboxField cb = (CheckboxField) f;
            if (cb.getChecked() == true) {
                MyObject mo = (MyObject) f.getCookie();
                System.out.println("USER = " + mo.refName + ", " + mo.name+ ", " + mo.dob);
                mm = null;
            }

}

 

This worked out  exactly as I want and as required.'

 

Thanks

 

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.