Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

setSelectedRow on a picker

Have anyone had any luck with making the setSelectedRow function work on a picker?

I have tried many times now, but no luck.

Tried to put it on a click event on another control in the same window, to make sure it wasn't something with the control not being loaded before trying to set the selected row.

Nothing seems to work, I have also tried to set it when initializing the pickerrows.

Any ideas anyone?

— asked November 12th 2010 by Ole Gade Nielsen
  • picker
  • setselectedrow

8 Answers

  • The one that worked for me was using an eventlistener on the window and call picker.setSelectedRow when it opens, like this:

    win.addEventListener('open', function(e){
        Ti.API.debug('win open event');
        picker.setSelectedRow(0, 2, false);

    Inspired by this:

    — answered November 21st 2011 by Dario Marcelino
    1 Comment
    • Thanks!

      — commented January 10th 2012 by Robert Stump
  • Based on your description, my first guess is that the setSelectedRow() method may be occurring before the picker object is fully instantiated. I'm still new to appcelerator, but it seems almost everything is asynchronous and just because you have called Ti.UI.createPicker() does not mean that subsequent lines of code can necessarily call methods in the object.

    So either try putting the setSelectedRow() inside a short delay via setTimeout(), or if you have other code before the picker becomes visible try moving the setSelectedRow() to later in the code block. Here is an exampmle of what you could try:

      var picker = Ti.UI.createPicker(); 
      setTimeout( function() { picker.setSelectedRow( col, row, true ); }, 250);

    That will insert a 1/4 second delay before the setSelectedRow() is executed. If that does not do it, try increasing it to 500ms or so and see what happens.

    In my limited experience so far, it seems timing interdependencies vary be device speed/OS level and account for some of what I see during testing that would otherwise seem like random crashes or code appearing to be ignored. I could be wrong, but it seems in part that could be due to objects not being fully instantiated when I try to access some properties or whatever too soon after the createXxx() call.


    — answered November 13th 2010 by Doug Handy
    • Hi.
      I tried it, but still no luck.
      It seems like it's releated to that the picker is in the popover control.
      I tried to put an eventlistener on a label in the popover view that called the setSelectedRow function, but that didn't help eighter :(

      If i move it to a normal view/window, it works perfectly. But I would really like to have it in the popover control.

      — commented November 13th 2010 by Ole Gade Nielsen
    • Amazing! This works for me. Make sure you get all the code in the code box. Some of the code was hidden in Chrome.

      — commented May 28th 2011 by Kenneth Lewis
  • Hi Ole

    Firstly, when posting a question it is very helpful to always state your target/development platform and your SDK version number, as it can make a big difference to the answer. :)

    Have you tried running picker_basic.js in the KS? Please let me know what happens when you do, along with the information I've requested above, and I will try to help you.


    — answered November 12th 2010 by Paul Dowsett
  • Oh sorry about that.

    I am developing for the iPad iOS 4.2 with titanium 1.4.2.

    I tried the picker_basic.js, and it worked perfectly. It seems like the issue is that i am using a popover control to show the picker.

    So the setup is:
    winMain contains a view with a button "Info".
    The popover is triggered with a click event on the info button.
    The content of the popover is set to a window, which contains a view with a picker.
    I added a button in the popover view with a listener which tries to set the selectedRow, but it doesn't respond (I know the function is executed, but nothing happens)

    Have you had any luck with making this work in a popover?

    Thank you for your response so far.


    — answered November 13th 2010 by Ole Gade Nielsen
  • the same problem here. I'm working on iPad with titanium 1.6.0 RC1. I need the picker inside a popover, but setSelectedRow does nothing. In my case is a multi-column picker.

    in kitchensink works fine, but the picker is not in a pop up. :(

    — answered February 23rd 2011 by Javier Rayon
  • SOLVED! disable animation, then works fine… (titanium 1.6.0RC1)

    yourPicker.setSelectedRow(colIndex,rowIndex, false);

    — answered February 23rd 2011 by Javier Rayon
  • Same problem here. I'm working with 1.6.2 on OSX and so far have only tried targeting the iPhone emulator. Here are the issues I am having.

    1) Since loading a single column of a multi-column picker looks to still be not working (see (http://developer.appcelerator.com/question/33881/picker-reloadcolumn-not-working) maybe fixed in 1.7?), in order to build a picker where the columns are interdependent, it is necessary to re-instance picker each time.

    2) After re-instancing the picker and adding columns, I need to reload the data. The problem is I have no idea when the picker instance is fully initialized. The following code demonstrates this problem:

    var column1= Ti.UI.createPickerColumn();
    var column2= Ti.UI.createPickerColumn();
    var picker = Ti.UI.createPicker({
        type: Ti.UI.PICKER_TYPE_PLAIN,
        animation: false
    Ti.API.info("columns: " +  this.picker.columns);
    Ti.API.info("columns: " +  this.picker.columns)

    With the following results

    [INFO] columns: [object TiUIPickerColumn]
    [INFO] columns: [object TiUIPickerColumn],[object TiUIPickerColumn]

    The problem is I need to reset the picker state after it is initialized, but cannot find a way to detect that. One solution is to set a timeout, but there no way to be sure the UI is completely done initializing, and it sounds like this interval will vary platform to platform. Is there perhaps an "oninit" event for the picker that I can catch. Another solution would be to make UI initialization synchronous with the context it is called from, but sound like that might be a deeper platform change.

    The only solution I seeing here is to set a reasonable timeout, cross your fingers, and hope the timeout is long enough. I myself am not comfortable without a solution.

    — answered May 4th 2011 by John Olmstead
    1 Comment
    • Actually, the code above demonstrates that the add() calls are asynchronous. The same is true of the createPicker() call.

      — commented May 5th 2011 by John Olmstead
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.