single pickerfactory for float value, I have a few details off

Hi, I'm incorporating a picker into my app so the user can select a declination while s/he is running the app. I run the picker menu with the following code from another menu option:
Ui.pushView(new DeclinationPicker(), new DeclinationPickerDelegate(), Ui.SLIDE_LEFT); // number picker...


The following is my DeclinationPicker
  • oh gee... this forum is giving me major issues :(
  • please see attached code.txt for question, it wouldn't let me post the text in the inline box, I would be most grateful if someone could fix my posting, I couldn't edit previous postings, nor could I include this attachment on previous postings.. sorry...

    NOTE: I think you need to be logged in to view the file, otherwise it may say "invalid content"
  • can anyone just tell me how defaults can be loaded into the picker initialization function? I can't understand the picker sample

  • You should have a closer look at the TimePicker part of the sample program. It does exactly what you need and a little more.

    According to the documentation, the value associated with the :defaults key will be

    An array of number objects indicating the starting index for each entry in the supplied pattern


    Your pattern has one entry (the number factory), so your defaults key will map to an array with a single value. That value will be the index of the value kept by the factory. You can figure out what that index is for a given value by asking the factory via the getIndex() method provided.

    I believe it will be something like this...

    class DeclinationPicker extends Ui.Picker {

    function initialize( ) {
    var title = new Ui.Text({
    :text=>"Declination",
    :locX =>Ui.LAYOUT_HALIGN_CENTER,
    :locY=>Ui.LAYOUT_VALIGN_BOTTOM,
    :color=>Gfx.COLOR_WHITE
    });

    var factory = new NumberFactory(-90, 90, 0.1, {:format=>"%0.1f"});

    var pattern = [
    factory
    ];

    var storedValue = App.getApp( ).getProperty("D");

    // the underscore at the start of this line is to trick the forum software to allow me to post
    // this code inline. just remove it.
    _if (storedValue == null) {
    storedValue = 0.0;
    }

    var defaults = [
    factory.getIndex(storedValue)
    ];

    Picker.initialize({:title=>title, :pattern=>pattern, :defaults=>defaults});
    }

    function onUpdate(dc) {
    dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_BLACK);
    dc.clear( );
    Picker.onUpdate(dc);
    }
    }


    class DeclinationPickerDelegate extends Ui.PickerDelegate {

    function initialize( ) {
    PickerDelegate.initialize( );
    }

    function onCancel( ) {
    Ui.popView(Ui.SLIDE_IMMEDIATE);
    }

    function onAccept(values) {

    // again, there is one entry in your pattern, so there will be one entry in
    // the values array.
    App.getApp( ).setProperty("D", values[0]);

    Ui.popView(Ui.SLIDE_IMMEDIATE);
    }
    }


    Now when you want to get the declination value in your code, you just access it from the app settings (App.getApp().getProperty("D")).

    Travis
  • Thank you Travis. Unfortunately it still behaved slightly incorrectly, which at first I thought was due to the negative numbers in my picker. The below "getIndex" function works correctly, however the one in the sample seemed to not work properly, as shown below.

    function getIndex(value) {
    var index;
    //index = ( value / mIncrement ) - mStart; // this line seems wrong
    index = (value - mStart) / mIncrement; // this one works
    return index;
    }


    thanks for your help! I think it is my computer I need to upgrade for the forum to let me post better.