How to set the default selected index of UIPickerView on Xamarin.iOS ?

I have created and populated a UIPickerView in iOS using Xamarin. I am using the following PickerViewModel...

`using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace someNamespace.iOS
{
///


/// This is our simple picker model. it uses a list of strings as it's data and exposes
/// a ValueChanged event when the picker changes.
///

public class PickerDataModel : UIPickerViewModel
{

    public event EventHandler<EventArgs> ValueChanged;

    /// <summary>
    /// The items to show up in the picker
    /// </summary>
    public List<string> Items
    {
        get { return items; }
        set { items = value; }
    }
    List<string> items = new List<string>();

    /// <summary>
    /// The current selected item
    /// </summary>
    public string SelectedItem
    {
        get { return items[selectedIndex]; }
    }
    protected int selectedIndex = 0;

    public void setSelectedIndex(int index)
    {
        this.selectedIndex = index;
    }

    /// <summary>
    /// default constructor
    /// </summary>
    public PickerDataModel ()
    {
    }

    /*
    public PickerDataModel (NSArray itemSourceArray, int selectedItemIndex)
    {
        for (int i = 0; i < itemSourceArray.Count; i++)
        {
            string singleItem = (string)itemSourceArray.GetItem[i];
            this.Items.Add(singleItem);
        }
        this.selectedIndex = selectedItemIndex;
    }
    */

    /// <summary>
    /// Called by the picker to determine how many rows are in a given spinner item
    /// </summary>
    public override int GetRowsInComponent (UIPickerView picker, int component)
    {
        return items.Count;
    }

    /// <summary>
    /// called by the picker to get the text for a particular row in a particular 
    /// spinner item
    /// </summary>
    public override string GetTitle (UIPickerView picker, int row, int component)
    {
        return items[row];
    }

    /// <summary>
    /// called by the picker to get the number of spinner items
    /// </summary>
    public override int GetComponentCount (UIPickerView picker)
    {
        return 1;
    }

    /// <summary>
    /// called when a row is selected in the spinner
    /// </summary>
    public override void Selected (UIPickerView picker, int row, int component)
    {
        selectedIndex = row;
        if (this.ValueChanged != null)
        {
            this.ValueChanged (this, new EventArgs ());
        }   
    }
}

}

`

And I am using the following code to show and populate the PickerView...

`
var pickerDataModel = new PickerDataModel ();
for (int i = 0; i < 7; i++)
{
string singleItem = "Item" + i;
pickerDataModel.Items.Add(singleItem);
}
controlStockPickerView.ShowSelectionIndicator = true;
controlStockPickerView.Model = pickerDataModel;
controlStockPickerView.Source = pickerDataModel;

// wire up the value change method
pickerDataModel.ValueChanged += (s, e) => {
Console.WriteLine("Selected Item: " + pickerDataModel.SelectedItem);
};
`

So far that goes quite well. The problem it, i am not able to set the default index. When the pickerView populates, it shows "Item 0" as default index(index 0). But i want to set the default index as my own. Such as "Index 2" or "Index 3". So how can i do that ?

I have tried with setting the following but that didn't work.

protected int selectedIndex = 3;

Can anyone show me the right path to achieve this ?
I am a beginner on Xamarin, so pardon me if i miss anything or if the question is too basic.

Thanks in advance.

Best Answer

Answers

  • NormanBardellNormanBardell AUMember ✭✭

    This works, but does not focus the picker on the correct option. How do we change the option focus as well?

  • madavermadaver Member
    edited February 24

    works but selected item is not seted, I set the selected item with controlStockPickerView.Model.Selected(controlStockPickerView, index, 1);

Sign In or Register to comment.