Spinner On Touch sometimes not working after SetList

281 views
Skip to first unread message

John Kenedy

unread,
Feb 26, 2016, 9:50:08 AM2/26/16
to DroidScript
spin = app.CreateSpinner([1,2,3,4,5,6], 0.2, HeaderHeight);
spin.SetOnChange(ChangePage);

then after a button
spin.SetList([1,2,3]);
spin.SetOnChange(ChangePage);

function ChangePage()
{
    //sometimes not getting called especially after call SetList
}

Dave Smart

unread,
Feb 26, 2016, 10:50:06 AM2/26/16
to DroidScript
Hi John,

We have a know problem with the spinner when setting the list before the spinner has been drawn to screen.  If you only set list after it is drawn, then you should be ok. Like this:-


//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );

//Create image 1/5 of screen width and correct aspect ratio.
img = app.CreateImage( "/Sys/Img/Hello.png", 0.2, -1 );
lay.AddChild( img );
//Create a button 1/3 of screen width and 1/10 screen height.
btn = app.CreateButton( "Press Me", 0.3, 0.1 );
btn.SetMargins( 0, 0.05, 0, 0 );
btn.SetOnTouch( btn_OnTouch );
lay.AddChild( btn );
spin = app.CreateSpinner([1,2,3,4,5,6], 0.2 );
    spin.SetOnChange(ChangePage);
    lay.AddChild( spin );
//Add layout to app.
app.AddLayout( lay );
}


function ChangePage( page )
{
    console.log( page );
}

//Called when user touches our button.
function btn_OnTouch()
{
spin.SetList([1,2,3]);
}



BareK

unread,
Jun 6, 2016, 12:32:47 PM6/6/16
to DroidScript
Hi Dave,

I'm facing the same problem as John.
But it my case, I set the list after the spinner has been drawn.

In fact, I update the existing list and re-set it to the spinner.

I made a very short example:

var List = [ "Elem 1.0", "Elem 1.1", "Elem 1.2", "Elem 1.3", "Elem 1.4" ];
var Spin;

function OnStart()
{    
    app
.SetOrientation( "Portrait" );
   
   
var lay = app.CreateLayout( "Linear", "FillXY,VCenter" );
   
   
Spin = app.CreateSpinner( List );    
   
Spin.SetOnChange( Spinner_OnChange );
   
Spin.SetMargins(0, 0, 0, 0.175 );
    lay
.AddChild( Spin );
   
   
var Btn = app.CreateButton( "Refresh", 0.95, 0.5 );
   
Btn.SetOnTouch( UpdateInfos );
    lay
.AddChild( Btn );
   
    app
.AddLayout( lay );
}

function Spinner_OnChange( item )
{
    app
.ShowPopup( item, "Short" );
   
   
if( item !== '' && item !== this.PreviousItem )
   
{
       
this.PreviousItem = item;
   
}
}

function UpdateInfos()
{
   
// Updates the whole list
   
Spin.SetList( List );
   
   
// Select back the previous selected item
   
Spin.SelectItem( Spin.PreviousItem );
}

The fact of using Spinner.SetList( list, delim ) seems to ignore the next OnTouch/OnChange event.
Very anoying in term of user experience :/

Hop it could be solved,
Cheers

Dave Smart

unread,
Jun 7, 2016, 6:08:43 AM6/7/16
to DroidScript
Hi BareK,

It is designed to work this way.  I can assure you that having callback events automatically triggered when changing GUI controls from your own code can cause all sorts of problems with recursion and infinitely chained events (many years of coding have taught me this :) 

If you are making a change to the control, then you must know what change you are making and are therefore in a position to trigger the expected callback yourself from code.

Regards
David

BareK

unread,
Jun 7, 2016, 8:49:36 AM6/7/16
to DroidScript
Hi Dave,

Thanks for your reply :)
I know that OnTouch/OnChange events automatically triggered when changing GUI controls from your own code, but what I mean is that if I touch the spinner with my finger and select a new (different) element, no OnChange is triggered the first time.

After further tests it seems to happen only on my device running Marshmallow (Android 4.0 and 4.1 are ok).
The problem comes of the combination of spinner.SetList() and spinner.SelectItem() (spinner.SetText() does the same).
If I remove one of these lines, OnChange is called as espected when a re-touch the spinner.
"Funny" thing: If I touch the "test" button 7 time in a row, 7 OnTouch/OnChange events of the spinner will be ignored.

I made a shorten sample allow people to test:

var List = [ "Droid", "Script", "Rulez" ];

function OnStart()
{  
   
var lay = app.CreateLayout( "Linear", "FillXY,VCenter" );

   
   
Spin = app.CreateSpinner( List );    
   
Spin.SetOnChange( Spinner_OnChange );
   
Spin.SetMargins(0, 0, 0, 0.175 );
    lay
.AddChild( Spin );
   
   
var Btn = app.CreateButton( "Refresh", 0.95, 0.5 );
   
Btn.SetOnTouch( UpdateInfos );
    lay
.AddChild( Btn );
   
    app
.AddLayout( lay );
}

function UpdateInfos()
{
   
Spin.SetList( List );
   
Spin.SelectItem( List[0] );

}

function Spinner_OnChange( item ) { app.ShowPopup( item, "Short" ); }


So I don't know if it is a problem with Marshmallow, CyanogenMod 13.0 (which I have) or only my phone specifically (OnePlus One).

I'll try tonight on a different OnePlus One with CM 13 and keep you in touch :)

Thanks

BareK

unread,
Jun 8, 2016, 7:24:18 AM6/8/16
to DroidScript
Tried on the second phone and same problem.
Still wondering if it's a CM 13 or just a Marshmallow issue :/
Reply all
Reply to author
Forward
0 new messages