Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear picker if It is selected in xamarin forms?

I have two picker. second picker is dependent on first picker. Both pickers are bind from Service. I am using dictionary object to bind data to picker. I am not using MVVM pattern.

  1. First service call where dictionary object for first picker is bind.
  2. then fill first picker from that dictionary object. At that time Second picker is empty.
  3. On selectedIndexChange event of first picker call service to bind dictionary object of second picker.
  4. Now Fill values to second picker from dictionary object. (If already picker has data then put Picker.Items.clear())
  5. Now If I select some value from second picker and change value of first picker then It gives me error at Picker.Items.clear()

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

Global Declaration :

Dictionary<string, string> DicObjActivityType;

Dictionary<string, string> DicObjSelfActivity;

First Picker selectedIndexChange event

private async void PckrActivityType_SelectedIndexChanged(object sender, EventArgs e)
{
    if (sender as Picker == null)
        return;
    else
    {
        var objActivityType = sender as Picker;
        var Key = DicObjActivityType.FirstOrDefault(x => x.Value == objActivityType.Items[objActivityType.SelectedIndex]).Key;
        PickedActivityType = Key;
        if (Key != "0")
        {
            PckrSelfActivity.IsEnabled = true;
            await CallGetWebService_SelfActivity(Key);
            if (PckrSelfActivity.IsEnabled == true)
            {
                PckrSelfActivity.Items.Clear();
                foreach (string items in DicObjSelfActivity.Values)
                {
                    PckrSelfActivity.Items.Add(items);
                }
            }
        }
        else
        {
            PckrSelfActivity.IsEnabled = false;
        }
    }
}

Call Service of second picker

private async Task CallGetWebService_SelfActivity(string strkey)
{
    try
    {
        var response = await GetResponseFromWebService.GetResponse<ServiceClasses.RootObject_LstListComboData>(ServiceURL.GetSelfActivity + "ActivityTypeCd=" + strkey);

        if (response.Flag == true)
        {
            DicObjSelfActivity = new Dictionary<string, string>();
            DicObjSelfActivity.Add("0", "--Select--");
            if (response.lstListComboData != null)
            {
                foreach (ServiceClasses.LstListComboData Items in response.lstListComboData)
                {
                    DicObjSelfActivity.Add(Items.Value, Items.Text);
                }
            }
        }
        else
        {
            PckrSelfActivity.IsEnabled = false;
        }
    }
    catch (Exception e)
    {
        await DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK");
    }
}

I refer following link to solve this issue

https://forums.xamarin.com/discussion/55922/picker-clear-system-argumentoutofrangeexception

but didn't find solution.

We can't clear picker if any of the value is selected?

I don't want to use BindablePicker cutom control.

like image 716
RMR Avatar asked Nov 18 '16 11:11

RMR


1 Answers

I'm not sure if by "clear the picker" you mean reset it such that it doesn't show any items as selected. But if that's what you want, then you can do the following:

  1. Set SelectedIndex = -1. This resets the picker such that no items are selected.

  2. Once you set SelectedIndex = -1, the selectedIndexChange event handler is called, and that's causing the

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Indeed yes, the SelectedIndex is -1, so it's complaining that the index must be non-negative.

  1. To avoid the ArgumentOutOfRangeException, add an if statement that encloses your SelectedIndexChange event handler so that the handler only executes
    if (SelectedIndexChange != -1).

In summary, do this:

YourPicker.SelectedIndex = -1;
YourPicker.SelectedIndexChanged += (sender, e) =>
{
    if (YourPicker.SelectedIndex != -1)
    {
        //Do your stuff
    }
}
like image 134
Fritz Lim Avatar answered Nov 16 '22 07:11

Fritz Lim