2
votes

hello i am working on xamarin forms 3.0 project and i am using picker to select values and using same for displaying user data to picker. Now my problem is that i am not able to show user selected value from database to picker.please help...

code behind

Below is the page constructor where i am passing my data to this page

public SummaryDetail(CpDetails cp)
        {
            InitializeComponent();
            if (cp == null)
            {
                throw new System.ArgumentNullException(nameof(cp));
            }
            GetLocations();
            BindingContext = cp;

            pklocation.SelectedIndex = cp.LocationId;
}
public async void GetLocations()
        {
        var loci = new List<Locations>();
        var client = new HttpClient();


        var json = await client.GetStringAsync("this is the link from where i am getting other values in picker");
        loci = JsonConvert.DeserializeObject<List<Locations>>(json);

        pklocation.ItemsSource = loci;
    }

Xaml is

<Picker x:Name="pklocation" Title="Select Location" ItemDisplayBinding="{Binding Name}"/>
3
GetLocations is async, but you are not awaiting itJason
Yah but still it is working my problem is that i am not able to show selected value in picker...Prasad Zende

3 Answers

2
votes

pklocation.SelectedIndex = cp.LocationId; This seems not correct.

The SelectedIndex means the index in the collection that is in the ItemsSource. It does not magically know which Id to select. So you need to either find out what the index of the selected object is in the list and set the SelectedIndex to that. But what is probably easier, is to set the SelectedItem: pklocation.SelectedItem = cp;

In case that doesn't work, try this:

pklocation.SelectedItem = ((List<Locations>)pklocation.ItemsSource).FirstOrDefault(c => c.LocationId == cp.LocationId);

Although then I would recommend saving the loci variable on a more global level so you can refer to it from your GetLocations method.

More information on the Microsoft Docs: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/picker/

0
votes

Thank you @Gerald Versluis for your help but i have done it by adding item using array and then i set SelectedIndex to selected value by comparing index in GetLocation() itself as below...

private int selectedLocation;
 public SummaryDetail(CpDetails cp)
        {
            InitializeComponent();

            GetLocations();
            BindingContext = cp;                
            selectedLocation = cp.LocationId;    
}
public async void GetLocations()
        {
            var loci = new List<Locations>();
            var client = new HttpClient();   

            var json = await client.GetStringAsync("this is link");
            loci = JsonConvert.DeserializeObject<List<Locations>>(json);
            foreach (var item in loci)
            {
                pklocation.Items.Add(item.Name);
                if (item.Id == selectedLoacion)
                    pklocation.SelectedIndex = item.Id;
            }

        }
0
votes

For me the Binding to the ViewModel worked ok like this.

XAML

<Picker x:Name="gender" Title="Select Gender" TitleColor="Blue" SelectedItem="{Binding Gender}">

ViewModel Code

public Object _gender;

        public Object Gender
    {
        get { return _gender; }
        set
        {
            _gender = value;
            OnPropertyChanged();
        }
    }

within function that retrieves data from database

Gender = (string)profile["gender"];