0

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.

Francis F
  • 3,157
  • 3
  • 41
  • 79

1 Answers1

0

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));
Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
  • I need to set the country at 100th index so I tried picker.select(0,100) but it shows the error - init has not been finished before PickerPrivate::selectedIndex(). Any advice?? – Francis F Dec 22 '14 at 10:00
  • picker ID goes from 0, while database ID goes from 1. So make sure you use 99 ID. Otherwise I haven't seen that error yet, maybe you try to set selected item before it's done? – Bojan Kogoj Dec 22 '14 at 11:24
  • I tried to set it oncreationcompleted of picker as well as the page also, and there are over 200 countries in the list so the 100th index will definitely be there. – Francis F Dec 22 '14 at 11:33
  • I'm not sure what the issue was but I set the picker value on timer with 100ms duration and oncreation complete I called the timer and it worked :) – Francis F Dec 22 '14 at 12:02
  • Also try in XmlDataModel if you can set selected in onItemAdded – Bojan Kogoj Dec 22 '14 at 12:12