2014-01-15 38 views
0

我在Xamarin android應用程序中使用了一個簡單的wcf web服務。我有GetAllUser函數來從數據庫中檢索所有用戶。用wcf web服務檢索數據綁定微調框

我可以使用我的Xamarin android應用程序插入用戶。 但我對如何綁定從Web服務檢索到的數據與Xamarin android中的微調控件無能爲力。 我在互聯網上搜索了很多地方,但我仍然沒有辦法綁定我的微調與網絡服務。

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; 

     } 

以上是我的Web服務,用於從數據庫中選擇所有用戶。 我想綁定微調器並在我的微調控件中顯示用戶名。

以下是我的按鈕單擊事件來填充微調框。

btn.Click += (sender, e) => { 

       Employee emp = new Employee(); 
       GridView grdvw = FindViewById<GridView>(Resource.Id.grd); 

       IList<Employee> employ = new List<Employee>(); 
       employ = sc.GetAllEmployee(""); 
       Spinner sp = FindViewById<Spinner>(Resource.Id.spinner); 

       sp.Adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleSpinnerItem ,employ.ToList()); 

      }; 

我能夠填充Web服務的微調,但在spineer 顯示的數據是這樣的:

"WcfServiceSunday.Employee" 

我在MTHE微調降得到這上面值下降,而不是名稱我的數據庫中有用戶 。

如何用用戶名填充微調框?

回答

1

除非您使用類似MvvmCross或QuickCross的數據進行數據綁定,否則無法將其綁定。但是,與Android一樣,您必須創建一個Adapter以填充View,例如ListView,SpinnerGridView與內容。這是相當容易的和瑣碎的事:

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; 
    } 
} 

spinneritem.axml:

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 
} 

<?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" /> 
與上面的代碼,你可以用下面簡單的代碼片段填充您的微調

所以

鑑於_serviceSunday是您的ServiceSunday類的一個實例。

您當然可以根據自己的喜好定製微調項目,爲其添加更多信息等等,只記得在Adapter中反映這些更改。

+0

我編輯了我的代碼並再次發佈。我想用我的數據庫中的用戶名填充我的微調器,但我將微調項目看作「WcfServiceSunday.Employee」。 –

+0

這可能是由於您將整個'Employee'對象而不是其'Name'屬性分配給'TextView'。查看我編輯的答案,或提供更多信息,說明您如何解決我在'GetDropDownView'中的'Text'屬性分配中引入的小型複製/粘貼錯誤。 – Cheesebaron

+0

我還沒有試過你的代碼。我一定會嘗試一下。我想知道如何使用我的代碼從「WcfServiceSunday.Employee」中選擇每個名稱。因爲我使用了相同的代碼來綁定網頁中的下拉列表。 Dropdownlist有「datavaluefield」和「datatextfield」分配給他們從他們工作的數據庫中獲取什麼值。這怎麼能在微調中完成呢? –