Titanium Community Questions & Answer Archive

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

Setting Picker Value

Hi, I have the following code:

var selectedRow=1;

var data = [];
data[0]=Titanium.UI.createPickerRow({title:'Choose One',value:3});
data[1]=Titanium.UI.createPickerRow({title:'Example 1',value:2});
data[2]=Titanium.UI.createPickerRow({title:'Example 2',value:1});

var field1 = Titanium.UI.createPicker({
    top:30,left:10,right:10,
});
field1.setSelectedRow(0,selectedRow);
field1.add(data);

I have set the values of each of the picker rows manually, when I come back to this page, I want the picker to be set the selected row so in this example would be 'Example 2' however it is using the index values so 'Example 1' is shown instead.

How can you do what I am trying above?

Thanks

— asked December 16th 2011 by Kevin Matthews
  • android
  • picker
0 Comments

1 Answer

  • setSelectedRow() will only look at the index of the column and row for setting the value. So, if you want to set the row to 'Example 2', which is in data[2], you would set the selected row to 2, as this is where teh row is located in data[].

    Since this is the case, you just need to keep track of the order the rows were placed in the array, and not about any property.

    — answered December 16th 2011 by Adam Paxton
    permalink
    3 Comments
    • Is there literally no way of doing it by data, so it checks the value the row has, then checks that against the value you want?

      I'll probably have to pull all of the data out, then assign each one a value based on the order they have come out in, then use this as the index! :(

      — commented December 16th 2011 by Kevin Matthews
    • Yes, you can:

      for(var i = 0; i < data.length; i++){
          if(data[i].value === 1) {
              selectedRow = i;
              break;
          }
      

      But I think that what you said about assigning each one a value based on the order they come in would be the better solution.

      — commented December 16th 2011 by Adam Paxton
    • Thank you Adam. I wish there was an index property to the picker so we can pick the value easier but your comment was helpful.

      — commented May 4th 2013 by Kam Rezvani
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.