2017-06-05 8 views
0

私はXamarin Android Appを持っており、チェックボックスでオプションのリストを表示して予期せぬ結果を得ようとしています。私はアダプタのGetViewメソッドでラムダイベントハンドラを作成しています。このようにして、GetViewへの現在の呼び出しの現在の項目は、CheckedChangedイベントが呼び出されたときに参照される項目になります。私のコードは次のとおりです。AlertDialogのListViewでCheckChangedイベントをキャプチャ

[Activity(Label = "ListViewCheckBox", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    public class Item 
    { 
     public string Name { get; set; } 
     public bool Checked { get; set; } 
    } 

    List<Item> Items; 

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

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     CreateItems(); 
    } 

    private void CreateItems() 
    { 
     Items = new List<Item>(); 

     for (int i = 0; i < 30; i++) 
     { 
      Item Item = new Item(); 
      Item.Name = "Question " + i.ToString(); 
      Items.Add(Item); 
     } 
    } 

    [Export("onButtonClicked")] 
    public void onButtonClicked(View view) 
    { 
     switch (view.Id) 
     { 
      case Resource.Id.btnShowDialog: 
       ShowDialog(); 
       break; 
     } 
    } 

    private void ShowDialog() 
    { 
     AlertDialog dlgList = (new AlertDialog.Builder(this)).Create(); 
     dlgList.SetTitle("Questions"); 
     var viewAD = this.LayoutInflater.Inflate(Resource.Layout.DialogList, null); 

     ListView lvItems = viewAD.FindViewById<ListView>(Resource.Id.lvDialog); 

     QuestionAdapter adItems = new QuestionAdapter(Items, this); 
     lvItems.Adapter = adItems; 

     dlgList.SetView(viewAD); 
     dlgList.SetButton("Close", delegate { }); 
     dlgList.Show(); 
    } 

    public class QuestionAdapter : BaseAdapter 
    { 
     private Activity context; 
     private List<Item> _Items; 

     public QuestionAdapter(List<Item> Questions, Activity context) 
     { 
      this.context = context; 
      _Items = Questions; 
     } 

     // How many items are in the data set represented by this Adapter. 
     public override int Count 
     { 
      get { return _Items.Count; } 
     } 

     // Get the data item associated with the specified position in the data set. 
     public override Java.Lang.Object GetItem(int position) 
     { 
      return position; 
     } 

     // Get the row id associated with the specified position in the list. 
     public override long GetItemId(int position) 
     { 
      return position; 
     } 

     // Get a View that displays the data at the specified position in the data set. 
     // You can either create a View manually or inflate it from an XML layout file. 
     public override View GetView(int position, View convertView, ViewGroup parent) 
     { 

      Item Item = _Items[position]; 

      if (convertView == null) 
      { 
       convertView = context.LayoutInflater.Inflate(Resource.Layout.DialogCheckListItem, null); 
      } 

      TextView t = convertView.FindViewById<TextView>(Resource.Id.txtItem); 
      t.Text = Item.Name; 

      CheckBox c = convertView.FindViewById<CheckBox>(Resource.Id.chkItem); 
      c.Checked = Item.Checked; 
      c.CheckedChange += (s, e) => 
      { 
       Item.Checked = e.IsChecked; 
      }; 

      return convertView; 
     } 
    } 
} 

私は一度警告ダイアログをポップアップし、いくつかのチェックボックスをオンにして閉じた場合。次回にポップアップすると、余分なボックスにチェックマークが付けられます。イベントが発生したときにそれを表す基本項目に作用するように、アダプターでイベントを配線する適切な方法は何ですか?誰かがこれを見て、すぐに私のエラーを指摘してくれることを願っています。もしそうでなければ、結果を見ることができる作業用のソリューションがここに掲載されています。 https://github.com/JimWilcox3/ListViewCheckBox

答えて

0

リストビューではビューがリサイクルされると、それに関連付けられたすべてのリスナーも表示されます。したがって、チェックボックスがチェックされていて、CheckedChangeがある場合、どちらも位置に基づいてリサイクル表示の一部として残ります。だから私は、以下のこのコードは、あなたのために働く必要がありますc.SetOnCheckedChangeListener(null);を追加することにより、chackedChangeをリセットします。

c.SetOnCheckedChangeListener(null); 
      t.Text = Item.Name; 
      c.Checked = Item.Checked; 
      c.CheckedChange += (s, e) => 
      { 
       CheckBox checkBox = (CheckBox)s; 
       if (checkBox.Checked) 
        Item.Checked = true; 
       else 
        Item.Checked = false; 
      }; 

はまた、あなたがダイアログを開くときに、常に何度も何度もアダプタを作成する別の30の項目を回避しようとそれが何をSI場合は追加excpetされます本当に欲しい。最初の項目に関連するバグもあります。

関連する問題