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

Native Development

Reply
Contributor
jurafloringheorghe
Posts: 16
Registered: ‎12-16-2010
My Device: Not Specified
Accepted Solution

checkbox infinite loop

I have a checkbox that's binded to a property.

When the user touches the checkbox the property will change so I can save it.

The problem is that onCheckedChanged is called whenever I modify the value.

I want to find a method to know when the user touched the checkbox but also have the property binding

 

How can I differentiate between onCheckedChanged and the moment I touch the checkbox on the screen

CheckBox {
            
             id:check
             checked: ListItemData.checking;
         
            onCheckedChanged : {
               ListItemData.checking= 0;//or do something to update my model
        
          
        }

 

Ps also when the checkBox is created onCheckedChanged is called if ListItemData.checking is ture

Please use plain text.
Developer
Zmey
Posts: 1,511
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: checkbox infinite loop

Try blocking the signals when changing "checked" from code. This thread should help:

http://supportforums.blackberry.com/t5/Cascades-Development/Problem-while-using-slider/m-p/2054759/h...


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Contributor
jurafloringheorghe
Posts: 16
Registered: ‎12-16-2010
My Device: Not Specified

Re: checkbox infinite loop

yes thanks for the fast answer this helps a lot but a problem still remains.

When the checkBox component is created OnChecked is called in and if the checkbox binded property is true it will mess my model up (making the propery value 0 ,when it should be 1)

 

A solution that solves the problem is declaring a property firstcreated and when onCretedCompleted is called initialize it with TRUE,Then in OnChecked check first if firstcreated is TRUE ...... then give it the value FALSE

 

If someone has a better ideea on how to fix this better I'm all ears :smileywink:

 

Please use plain text.
Developer
Zmey
Posts: 1,511
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: checkbox infinite loop

Why not take the state of a checkbox directly from dataModel? I think it's not neccessary to store it in a separate property.

Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: checkbox infinite loop

It sounds like you're using this inside a ListView. Look into using the ListItem.initialized property to ignore calls to onCheckedChanged when the visual item is just being set up (or reused, since they are re-assigned to other items when you're scrolling and they move offscreen).

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
raj_jyani
Posts: 107
Registered: ‎05-11-2011
My Device: BlackBerry Z10 LE
My Carrier: AirTel

Re: checkbox infinite loop

Hi ,

You can maintain flag variable for check wheather it is first time or not.

 

CheckBox {
            property bool flag: true
            text: "Button"
            onCheckedChanged: {
                if (flag) {
                    flag = false;
                } else {
                    //write you code here
                }
            }
        }

 flag variable like a private member variable of checkbox class.

so when row generate it will make different copy of flag viriable for every checkbox object.

 

 

 

Thank You.

Please use plain text.
Contributor
jurafloringheorghe
Posts: 16
Registered: ‎12-16-2010
My Device: Not Specified

Re: checkbox infinite loop

[ Edited ]

@Raj Unfourtunately this won't work because some of the elements won't be checked first time so onchecked won't be called ,and if you check them afterwards because of the flag they won't be checked on(in the model).

I'm using a list forgot to mention that , peter9477 solution works great.

 

Thanks everyone

Please use plain text.