2012-05-01 95 views
1

我有一個微調器,我正在填充表中的數據。所以我使用了一個定製的適配器,因爲我需要能夠顯示一列,但我將存儲在表中的數據來自另一列。假設我有一張叫做顏色的表格。它有一個ID字段和Desc字段。因此,當您選擇需要獲取該顏色的ID並存儲它的顏色時,我的微調器將顯示紅色,綠色,藍色和黑色。我有微調器製作並且工作正常。我創建了一個custome適配器來拉入這裏是我的適配器。如何使用自定義適配器將微調器設置爲特定值

class SpinnerAdapter : BaseAdapter 
{ 
    private IEnumerable<Color> _Color; 
    private Activity _context; 

    public SpinnerAdapter(Activity context, IEnumerable<Color> Color) 
    { 
     _context = context; 
     _Color = Color; 
    } 

    public override View GetView(int position, View convertView, 
           ViewGroup parent) 
    { 
     var view = (convertView ?? _context.LayoutInflater.Inflate(
      Resource.Layout.SpinnerList, parent, false)) as LinearLayout; 
     var Color = _Color.ElementAt(position); 
     view.FindViewById<TextView>(Resource.Id.ID).Text = 
      Color.ColorCd.ToString(); 
     view.FindViewById<TextView>(Resource.Id.TimeID).Text = 
      Color.ColorDesc.ToString(); 

     return view; 
    } 

    public override int Count 
    { 
     get { return _Color.Count(); } 
    } 

    public Color GetColor(int position) 
    { 
     return _Color.ElementAt(position); 
    } 

    public override Java.Lang.Object GetItem(int position) 
    { 
     return null; 
    } 
    public override long GetItemId(int position) 
    { 
     return position; 
    } 
} 

在我的活動頁面中,我將適配器設置爲像這樣的微調;

Spinner spAdapter = FindViewById<Spinner>(Resource.Id.spAdapter); 
spAdapter.ItemSelected += new EventHandler<ItemEventArgs>(spAdapter_ItemClick); 
var Color = ((LeavApplication)Application).LeaveRepository.GetAllColor(); 
spAdapter.Adapter = new SpinnerAdapter(this, Color); 

這部分工作正常。但如果我有價值,我希望微調設置爲如何做。例如,我想將微調器的值設置爲「藍色」,我如何找到藍色的位置以便我可以使用SetSelection函數來設置微調器。是否有我需要在我的適配器中創建的功能,它會是什麼。

回答

0

我想你可以自己使用顏色列表,然後可以在微調器上使用SetSelection?

例如爲:

Spinner spinner = FindViewById<Spinner>(Resource.Id.spAdapter); 
    spinner.ItemSelected += new EventHandler<ItemEventArgs>(spAdapter_ItemClick); 
    var colors = ((LeavApplication)Application).LeaveRepository.GetAllColor().ToList(); 
    spinner.Adapter = new SpinnerAdapter(this, colors); 
    spinner.SetSelection(colors.IndexOf(TheColorIWant)); 
+0

這沾到的IndexOf錯誤的函數:錯誤是「Android.widget.spinner」沒有包含IndexOf的定義,也沒有擴展方法'IndexOf'接受類型爲Android.widget.spinner的第一個參數 – mlwright

+0

我配置的代碼有'colors.IndexOf',而不是'spinner.IndexOf'? – Stuart

0

我想通了,正在爲我一個答案。在我的自定義適配器「SpinnerAdapter.cs」中,我添加了一個函數。

public int GetPosition(string value) 
{ 
    int i = 0; 
    foreach (var item in _Color) 
    { 
    if (item.ColorDesc == value) 
    { 
     return i; 
    } 
    ++i; 
    } 
    return 0; 
} 

然後在我的活動頁面中,我可以添加這個。

Spinner spAdapter = FindViewById<Spinner>(Resource.Id.spAdapter); 
    spAdapter.ItemSelected += new EventHandler<ItemEventArgs>(spAdapter_ItemClick); 
    var Color = ((LeavApplication)Application).LeaveRepository.GetAllColor(); 
    spAdapter.Adapter = new SpinnerAdapter(this, Color); 

    // Set the spinner with preDefined value 
    string sColor = "Green"; 
    int iPosition = ((SpinnerAdapter)spAdapter.Adapter).GetPosition(sColor); 
    FindViewById<Spinner>(Resource.Id.spAdapter).SetSelection(iPosition); 

並且它將所選項目設置爲預定顏色。

0

您應該使用下面的方法在主體

 int itemPosition = spinnerConsultationDegreeAdapter.GetItemPosition(request.ConsultationDegreeId); 
     spinnerConsultationDegree.SetSelection(itemPosition); 

然後加入相應的適配器

public int GetItemPosition(int consultationDegreeId) 
    { 
     var degree = _consultationDegrees.FirstOrDefault(q => q.Id == consultationDegreeId); 
     var position = _consultationDegrees.IndexOf(degree); 
     return position; 
    }