2017-08-13 106 views
1

我想知道是否有一種方法可以做出實際上能夠返回選定索引的多選列表視圖。我已經能夠使用預先製作的multiplechoicelistview適配器來做到這一點,但我需要能夠編輯它的風格。所以我需要一個自定義的listview。 這是我的OnCreate代碼Xamarin自定義選擇列表視圖

protected override void OnCreate(Bundle savedInstanceState) 
    { 

     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.Options); 
     outList = FindViewById<ListView>(Resource.Id.outList); 
     var btnCheck = FindViewById<ImageButton>(Resource.Id.btnConfirm); 
     var btnBack = FindViewById<ImageButton>(Resource.Id.btnBack); 
     for (int i = 0; i < NewProfileVars.LifeStyles.Length; i++) 
     { 
      inList.Add(NewProfileVars.LifeStyles[i].Name); 
     } 


     //list contents end here 

     ListViewAdapter adapter = new ListViewAdapter(this, inList); 
     outList.Adapter = adapter; 
     outList.ChoiceMode = ChoiceMode.Multiple; 
     NewProfile main = new NewProfile(); 
     btnCheck.Click += Confirm; 
     btnBack.Click += Back; 


    } 

這裏是我的列表視圖適配器代碼

class ListViewAdapter: BaseAdapter<string> 
    { 
     public List<string> Items; 
     public Context Context; 

     public ListViewAdapter(Context context, List<string> items) 
     { 
      Items = items; 
      Context = context; 
     } 


     public override int Count 
     { 
      get { return Items.Count; } 
     } 

     public override long GetItemId(int position) 
     { 
      return position; 

     } 
     public override string this[int position] 
     { 
      get { return Items[position]; } 
     } 

     public override View GetView(int position, View convertView, ViewGroup parent) 
     { 
      View row = convertView; 

      if (row == null) 
      { 
       row = LayoutInflater.From(Context).Inflate(Resource.Layout.ListBox, null, false); 

      } 
      CheckBox txtName = row.FindViewById<CheckBox>(Resource.Id.cbName); 
      txtName.Text = Items[position]; 
      return row; 
     } 
    } 

所有我現在需要的是要弄清楚是如何確認鍵可以節省我選擇的事情。 非常感謝您的幫助。

回答

4

我看到你在你的ListView中使用CheckBox。你可以得到的物品,其中Checked使用這樣的事情:

首先創建一個類,將舉行您的項目數據和檢查的狀態,爲例子,讓我們把它叫做

public class LifeStylesListItem 
{ 
    public string Name { get; set; } 

    public bool IsSelected { get; set; } 

    public LifeStylesListItem(string name) 
    { 
     Name = name; 
    } 
} 

然後修改ListViewAdapter

添加新的私人領域,將持有的LifeStylesListItem

private List<LifeStylesListItem> _list; 

列表初始化與T名單他的項目在構造函數中傳遞。

public ListViewAdapter(Context context, List<string> items) 
{ 
    Items = items; 
    _list = new List<LifeStylesListItem>(); 

    //Your are creating a copy of your Items 
    foreach (var item in items) 
    { 
     _list.Add(new LifeStylesListItem(item)); 
    } 

    Context = context; 
} 

GetView方法訂閱CheckedChange事件的CheckBox的。通過這種方式,當您檢查狀態發生變化時您將會收到通知。您還需要根據Item IsSelected值設置Checked屬性。當ListView將重用您的單元時,這是必需的。

public override View GetView(int position, View convertView, ViewGroup parent) 
{ 
    View row = convertView; 

    if (row == null) 
    { 
     row = LayoutInflater.From(Context).Inflate(Resource.Layout.ListBox, null, false); 

    } 
    CheckBox txtName = row.FindViewById<CheckBox>(Resource.Id.cbName); 
    txtName.Text = _list[position].Name; 
    txtName.Checked = _list[position].IsSelected; 

    txtName.CheckedChange -= TxtName_CheckedChange; 
    txtName.CheckedChange += TxtName_CheckedChange; 

    return row; 
} 

添加事件處理TxtName_CheckedChange方法

void TxtName_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e) 
{ 
    //These lines are used to get the position of the control that was clicked 
    var obj = sender as CheckBox; 
    var row = obj?.Parent as View; 
    var parent = row?.Parent as ListView; 

    if (parent == null) 
    { 
     return; 
    } 

    var position = parent.GetPositionForView(row); 

    // Once you have the position you can get the item and change 
    // its IsSelected 
    var item = _list[position]; 
    item.IsSelected = e.IsChecked; 
} 

然後,最後選擇在適配器添加是一個將返回選定的項目。在Linq的幫助下(需要添加using System.Linq),您可以像這樣查詢選定的項目。

public List<string> GetCheckedItems() 
{ 
    return _list 
      .Where(a => a.IsSelected) 
      .Select(b => b.Name) 
      .ToList(); 
} 

現在在你的活動,你只需要從確認按鈕點擊ListViewAdapter方法調用GetCheckedItems

private void Confirm(object sender, EventArgs e) 
{ 
    var checkedItems = adapter.GetCheckedItems(); 
} 

記住更改adapter私人領域的活動

private ListViewAdapter adapter; 

希望這會有所幫助.-

+1

兄弟你是一個巫師比非常感謝你這麼多年來一直困擾着我。列表項已經屬於一個布爾結構,所以我只是讓列表視圖適配器使用該結構。 但是非常感謝你。 –

+0

很高興工作! – apineda

+1

等待你知道它是如何工作的單選單選按鈕? –