Xamarin.android Monodroid中的微调器控件-选择时获取ID和文本

Xamarin.android Monodroid中的微调器控件-选择时获取ID和文本,xamarin.android,spinner,Xamarin.android,Spinner,我刚刚开始探索微调器控件。我几乎实现了我想要的,但只差最后一步了。以下是我迄今为止所做的工作 对于这个例子,我有一个非常简单的类: [Serializable] public class Merchant { public Int64 MerchantId { get; set; } public String ShopName { get; set; } public override string ToString() { return Sh

我刚刚开始探索微调器控件。我几乎实现了我想要的,但只差最后一步了。以下是我迄今为止所做的工作

对于这个例子,我有一个非常简单的类:

[Serializable]
public class Merchant
{
    public Int64 MerchantId { get; set; }
    public String ShopName { get; set; }

    public override string ToString()
    {
        return ShopName;
    }
}
这是我放置微调器的axml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="1280dip"
    android:layout_height="800dip">
    <TextView
        android:text="Select a merchant:"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lblSelect"
        android:layout_marginLeft="30dip"
        android:layout_marginTop="30dip"
        android:textSize="42dip" />
    <Spinner
        android:id="@+id/spinMerchant"
        android:layout_width="1000dip"
        android:layout_height="wrap_content"
        android:layout_below="@id/lblSelect"
        android:prompt="@string/spinner_prompt"
        android:layout_centerHorizontal="true"
        android:minHeight="20dip" />
</RelativeLayout>

我的代码是:

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Create your application here
        this.SetContentView(Resource.Layout.MerchantSelect);

        List<Merchant> lstMerchant = new List<Merchant> ();
        lstMerchant.Add (new Merchant() { ShopName = "First Shop", MerchantId = 11 });
        lstMerchant.Add (new Merchant() { ShopName = "Second Shop", MerchantId = 12 });

        Spinner spinner = this.FindViewById<Spinner>(Resource.Id.spinMerchant);
        ArrayAdapter adapter = new ArrayAdapter (this, Android.Resource.Layout.SimpleSpinnerItem, lstMerchant);
        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (spinner_ItemSelected);

        spinner.Adapter = adapter;
    }

    private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;
        //Merchant merch = (Merchant)spinner.SelectedItem;
        string toast = string.Format ("Selected text is {0}", spinner.GetItemAtPosition (e.Position));
        Toast.MakeText (this, toast, ToastLength.Long).Show ();
    }
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
//在此处创建应用程序
这个.SetContentView(Resource.Layout.MerchantSelect);
List LSTMANTER=新列表();
添加(新商户(){ShopName=“First Shop”,MerchantId=11});
添加(新商户(){ShopName=“Second Shop”,MerchantId=12});
微调器微调器=this.FindViewById(Resource.Id.spinMerchant);
ArrayAdapter=新的ArrayAdapter(这个,Android.Resource.Layout.SimpleSpinnerItem,lstMerchant);
spinner.ItemSelected+=新事件处理程序(spinner\u ItemSelected);
spinner.Adapter=适配器;
}
私有无效微调器\u选定项(对象发送者,AdapterView.ItemSelectedEventArgs e)
{
微调器微调器=(微调器)发送器;
//Merchant merch=(Merchant)spinner.SelectedItem;
string toast=string.Format(“所选文本为{0}”,spinner.GetItemAtPosition(e.Position));
Toast.MakeText(this,Toast,ToastLength.Long).Show();
}
我想得到选定的文字,以及它背后的ID尽快作出选择。我通过
spinner.GetItemAtPosition(e.Position)
获取文本,但我似乎找不到任何可以给我ID的东西。如果我尝试执行
Merchant merch=(Merchant)spinner.SelectedItem我得到一个异常:
无法将类型“Java.Lang.Object”转换为“Merchant”

请让我知道如何实现这一目标


谢谢。

微调器只知道文本,因为这是您的
ToString()
方法中的文本。提供微调器数据的适配器不知道它正在处理
Merchant
对象。 要访问实际的商户对象,您必须将您的
列表lstmchant
设置为您的类的成员

然后更改选择处理程序:

private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;
       // Get the ID from your model.
        Merchant merch = this.lstMerchant[e.Position];
        var id = merch.MerchantId;
        string toast = string.Format ("Selected text is {0}", spinner.GetItemAtPosition (e.Position));
        Toast.MakeText (this, toast, ToastLength.Long).Show ();
    }

谢谢你,伙计!虽然我希望有一个更“框架支持”的解决方案,但这也能奏效。