使用从wcf web服务检索的数据绑定微调器

使用从wcf web服务检索的数据绑定微调器,wcf,web-services,xamarin.android,xamarin,xamarin-studio,Wcf,Web Services,Xamarin.android,Xamarin,Xamarin Studio,我在Xamarin android应用程序中使用了一个简单的wcf web服务。我有GetAllUser函数从数据库中检索所有用户 我可以使用我的Xamarin android应用程序插入用户。 但我不知道如何在我的Xamarin android中使用spinner控件绑定从web服务检索的数据。 我在互联网上搜索了很多地方,但我仍然无法将我的微调器与web服务绑定 namespace WcfServiceSunday { public class ServiceSunday : ISe

我在Xamarin android应用程序中使用了一个简单的wcf web服务。我有GetAllUser函数从数据库中检索所有用户

我可以使用我的Xamarin android应用程序插入用户。 但我不知道如何在我的Xamarin android中使用spinner控件绑定从web服务检索的数据。 我在互联网上搜索了很多地方,但我仍然无法将我的微调器与web服务绑定

namespace WcfServiceSunday
{
    public class ServiceSunday : IServiceSunday
    {

        public  List<Employee> GetAllEmployee()
        {
            List<Employee> employee = new List<Employee>();
            string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            con.Open();
            string qry = "select * from EmpDetail";
            SqlCommand cmd = new SqlCommand(qry, con);

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Employee emp = new Employee();
                emp.EmpNo = dr.GetInt32(0);
                emp.Name = dr.GetString(1);
                emp.Empcode = dr.GetString(2);
                emp.keywords = dr.GetString(3);
                emp.mobile = dr.GetString(4);

                employee.Add(emp);

            }
            con.Close();
            return employee;

        }
我在上面得到的是微调器下拉列表中的值,而不是用户名称 在我的数据库里


如何使用用户名填充微调器???

除非使用MvvmCross或QuickCross之类的工具进行数据绑定,否则无法绑定微调器。然而,与Android一样,您必须创建一个
适配器来填充
视图
,例如
列表视图
微调器
网格视图
。这是相当容易和琐碎的:

public class MySpinnerAdapter : ArrayAdapter<Employee>
{
    private readonly int _resource;
    private readonly int _resourceId;

    public MySpinnerAdapter(Context context, int resource, int textViewResourceId, IList<Employee> objects)
        : base(context, resource, textViewResourceId, objects)
    {
        _resource = resource;
        _resourceId = textViewResourceId;
    }

    public override View GetDropDownView(int position, View convertView, ViewGroup parent)
    {
        var inflater = LayoutInflater.FromContext(Context);
        var view = convertView ?? inflater.Inflate(_resource, parent, false);
        var textView = view.FindViewById<TextView>(_resourceId);
        textView.Text = GetItem(position).Name;
        return view;
    }
}
公共类MySpinnerAdapter:ArrayAdapter
{
私有只读int_资源;
私有只读int_resourceId;
公共MySpinnerAdapter(上下文上下文、int资源、int文本视图资源ID、IList对象)
:base(上下文、资源、textViewResourceId、对象)
{
_资源=资源;
_resourceId=textViewResourceId;
}
公共覆盖视图GetDropDownView(int位置、视图转换视图、视图组父视图)
{
var充气器=LayoutInflater.FromContext(上下文);
var视图=convertView??充气机。充气(_资源,父项,false);
var textView=view.FindViewById(_resourceId);
Text=GetItem(position).Name;
返回视图;
}
}
spinneritem.axml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerText"
    android:ellipsize="marquee"
    android:textAppearance="?android:attr/textAppearanceLarge"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:color="#ffffff" />

因此,通过上面的代码,您可以使用以下简单代码片段填充微调器:

var spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.Adapter = new MySpinnerAdapter(this, Resource.Layout.spinneritem, Resource.Id.spinnerText, _serviceSunday.GetAllEmployee());
spinner.ItemSelected += (s, e) => 
{
    //Do something with the selected item
    //get the position with e.Position
}
var spinner=findviewbyd(Resource.Id.spinner);
spinner.Adapter=new-MySpinnerAdapter(这个,Resource.Layout.spinneritem,Resource.Id.spinnerText,_-serviceSunday.GetAllEmployee());
spinner.ItemSelected+=(s,e)=>
{
//对所选项目执行某些操作
//用e.position获得位置
}
假定
\u serviceSunday
是您的
serviceSunday
类的一个实例


当然,您可以根据自己的喜好自定义微调器项目,向其添加更多信息等。只需记住在
适配器中反映这些更改

我已编辑了我的代码并再次发布。我想用数据库中的用户名填充微调器。但我看到微调器的项目为“WcfServiceSunday.Employee”。这可能是因为您将整个
Employee
对象,而不是其
Name
属性分配给
文本视图
。查看我编辑过的答案,或者提供更多信息,说明如何修复我在
GetDropDownView
中的
Text
属性分配中引入的小复制/粘贴错误。我尚未尝试您的代码。我一定会试试的。我想知道如何使用我的代码从“WcfServiceSunday.Employee”中选择每个名称。因为我使用了相同的代码在网页中绑定我的dropdownlist。Dropdownlist有“datavaluefield”和“datatextfield”两个字段,为它们指定从工作的数据库中获取的值。如何在spinner中实现这一点?如果spinner可以在其项目列表中填充“WcfServiceSunday.Employee”,那么应该有一种方法使其显示特定的值,而不是整个Employee对象?等待您的回答(谢谢您的回答,是的;)最后我没有使用循环就解决了它。使用where查询简单地遍历我的整个列表。谢谢你的努力。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerText"
    android:ellipsize="marquee"
    android:textAppearance="?android:attr/textAppearanceLarge"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:color="#ffffff" />
var spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.Adapter = new MySpinnerAdapter(this, Resource.Layout.spinneritem, Resource.Id.spinnerText, _serviceSunday.GetAllEmployee());
spinner.ItemSelected += (s, e) => 
{
    //Do something with the selected item
    //get the position with e.Position
}