Search code examples
blackberryqmlblackberry-10pickerblackberry-cascades

Set default value in custom picker in blackberry cascades using qml


I have created a picker that shows list of countries and it works fine. Currently by default the first country is selected. I need to change it to another country. How should I do that?

This is the picker I created

 Picker {
            id: picker
            title: "Select your country"
            expanded:true
            verticalAlignment: VerticalAlignment.Fill
            // A DataModel is used to populate the picker.
            dataModel: XmlDataModel {
                source: "Model/CountryCodes.xml"
            }

            // Picker items are set up similarly as to how its done in a ListView.
            pickerItemComponents: [
                PickerItemComponent {
                    type: "country"
                    content: Container {
                        background: Color.create("#9B59B6")
                        layout: DockLayout {
                        }
                        Label {
                            multiline: true
                            maxWidth:  1000
                            text: pickerItemData.name
                            verticalAlignment: VerticalAlignment.Center
                            horizontalAlignment: HorizontalAlignment.Center

                            textStyle {

                                base:SystemDefaults.TextStyles.BodyText
                                color: Color.White
                                fontSize: FontSize.PointValue
                                fontSizeValue: 10.0
                                fontWeight: FontWeight.Normal
                            }
                        }
                    }
                }
            ]

                       onSelectedValueChanged: {
                // These are the currently selected indexes.
                var index0 = picker.selectedIndex(0);
                var type = dataModel.data([0, picker.selectedIndex(0)]);

                console.log("Selected index:"+index0);
                countryCode.mainText="+"+type.phoneCode;
            }    

        } 

My countrycodes.xml is in this format

  <model>
  <column loop="false" colspan="3" >
  <country code='af' phoneCode='93' name='Afghanistan' />
  <country code='al' phoneCode='355' name='Albania' />
  ........all other countries.....
  </column> 
  </model>

Currently Afganistan shows selected all the time.


Solution

  • I used this, it loads which one from settings. This is placed in onCreationCompleted

    picker.select(0, app.getValueFor("backImageID", 0));
    

    and the second element is like this

    picker.select(1, app.getValueFor("fontColorID", 0));