My guess is that you are reusing the ConvertView cells in: public override View GetView(int position, View convertView, ViewGroup parent)
You only need to subscribe to the events the first time you create each "row". When you scroll down/up and Android reuses the rows, then you need to work out how to reprogram the event handlers It might be useful for you to use Tag properties in this case - to store the reference to the current object attached to the "row" Stuart On 7 June 2012 09:55, Marcus <m...@wise.no> wrote: > Hi, new to Mono for Android. Have problem with Click event in Custom > Adapter. > I am using a custom adapter for a ListView where the layout states a button > and a CheckBox on each row. Upon clicking the button, the text on the > button will change. Button and CheckBox is connected to .Click event inside > GetView. > > Problem is, when clicking one button or checkbox, several click events for > several buttons/checkboxes on different rows(positions) will fire, one > after > the other. This will happen when clicking some, but not all > buttons/checkboxes(position 0 always bugs). > I have worked allot with custom adapters in Java(Android) and have never > seen this problem before. A bug or my code is faulty? Any thoughts on > solution?? > Using VS 2010 and Honeycomb project with latest updates for VS2010 and > Monodroid. > > > Custom Adapter: > > public class IconAdapter : BaseAdapter > { > Activity cont; > SplitActivity outerClass; > public IconAdapter(Activity cont, int textViewResourceId, > SplitActivity outerClass) > :base() > { > this.cont = cont; > this.outerClass = outerClass; > } > > public override int Count { > get { return outerClass.items.Count; } > } > > public override Java.Lang.Object GetItem (int position) { > return null; > } > > public override long GetItemId (int position) { > return position; > } > > public override View GetView(int position, View convertView, > ViewGroup parent) > { > var item = outerClass.items[position]; > // var row = (convertView ?? > cont.LayoutInflater.Inflate(Resource.Layout.main3, parent, false)) as > LinearLayout; > View row = convertView; > if (row == null) > { > LayoutInflater inflater = > (LayoutInflater)cont.GetSystemService(Context.LayoutInflaterService); > row = > inflater.Inflate(Resource.Layout.main3, null, false); > } > CheckBox check = > row.FindViewById<CheckBox>(Resource.Id.checkBox1); > Button amount = > row.FindViewById<Button>(Resource.Id.amountEdit); > > check.Text = " " + item.tagId; > check.Checked = item.check; > amount.Text = item.amount; > > check.Click += (sender, e) => > { > Toast.MakeText(cont, "Checked" + position, > ToastLength.Short).Show(); > }; > > amount.Click += (sender, e) => > { > outerClass.onNewAmount(position); > }; > return (row); > } > } > > > Layout for ListView: > > <?xml version="1.0" encoding="utf-8"?> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" > android:layout_width="match_parent" > android:layout_height="match_parent"> > > <CheckBox > android:id="@+id/checkBox1" > android:layout_weight="2" > android:layout_width="0dp" > android:layout_height="wrap_content" > android:padding="20dp" > android:textSize="30dp" > android:text="CheckBox" /> > <Button > android:id="@+id/amountEdit" > android:layout_weight="1" > android:layout_width="0dp" > android:layout_height="wrap_content" > android:padding="20dp" > android:textSize="20sp" > /> > </LinearLayout> > > > Main Class: > > namespace TagApp > { > [Activity(Label = "My Activity")] > public class SplitActivity : Activity > { > List<TagDetails> items; > CheckBox checkAll; > private Button storeBtn; > ListView lv; > IconAdapter iconadapt; > > protected override void OnCreate(Bundle bundle) > { > base.OnCreate(bundle); > SetContentView(Resource.Layout.main4); > items = new List<TagDetails>(); > lv = FindViewById<ListView>(Resource.Id.my_list); > storeBtn = FindViewById<Button>(Resource.Id.storeBtn); > checkAll = FindViewById<CheckBox>(Resource.Id.checkBoxAll); > setupList(); > addToAdapter(); > } > > public void addToAdapter() > { > iconadapt = new IconAdapter(this, Resource.Layout.main3, this); > lv.Adapter = iconadapt; > } > > public void onNewAmount(int position) > { > int inInt = Convert.ToInt32(items[position].amount); > inInt++; > string sInt = "" + inInt; > items[position].amount = sInt; > iconadapt.NotifyDataSetChanged(); > } > > public class IconAdapter : BaseAdapter > { > ............ > } > > public void setupList() > { > items.Add(new TagDetails() {Id = 0, amount = "8", check = > true}); > items.Add(new TagDetails() {Id = 0, tagId = "tag0", > amount = "8", check = > true}); > items.Add(new TagDetails() {Id = 1, tagId = "tag1", > amount = "5", check = > true}); > items.Add(new TagDetails() {Id = 2, tagId = "tag2", > amount = "2", check = > true}); > items.Add(new TagDetails() {Id = 3, tagId = "tag3", > amount = "11", check > = true}); > items.Add(new TagDetails() {Id = 4, tagId = "tag4", > amount = "68", check > = true}); > items.Add(new TagDetails() {Id = 5, tagId = "tag5", > amount = "0", check = > true}); > } > > class TagDetails > { > public int Id { get; set; } > public string tagId { get; set; } > public string amount { get; set; } > public Boolean check { get; set; } > } > } > } > > > Main Class Layout: > > <?xml version="1.0" encoding="utf-8"?> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" > android:orientation="vertical" > android:layout_width="match_parent" > android:layout_height="match_parent"> > > <LinearLayout > style="@android:style/ButtonBar" > android:gravity="center_horizontal" > android:layout_height="wrap_content" > android:layout_width="match_parent"> > > <Button > android:id="@+id/storeBtn" > android:layout_width="match_parent" > android:layout_height="wrap_content" > android:text="STORE" > android:layout_marginLeft="30dp" > android:layout_marginBottom="5dp" > android:layout_marginRight="30dp" > android:background="#ffffff00" > android:textColor="#ff000000" > android:textSize="30sp" > /> > </LinearLayout> > > <LinearLayout > android:layout_height="wrap_content" > android:layout_width="match_parent"> > > <CheckBox > android:id="@+id/checkBoxAll" > android:layout_weight="2" > android:layout_width="0dp" > android:layout_height="wrap_content" > android:padding="20dp" > android:textSize="30dp" > android:checked="true" > android:text=" Select All" /> > > <TextView > android:id="@+id/descriptionText" > android:layout_weight="1" > android:layout_width="0dp" > android:layout_height="wrap_content" > android:textSize="30dp" > android:text="Amount" > /> > </LinearLayout> > > <LinearLayout > android:layout_height="wrap_content" > android:layout_width="match_parent"> > > <ListView > android:id="@+id/my_list" > android:layout_width="match_parent" > android:layout_height="match_parent"> > </ListView> > </LinearLayout> > </LinearLayout> > > > -- > View this message in context: > http://mono-for-android.1047100.n5.nabble.com/Click-event-in-Custom-Adapter-fire-multiple-positions-tp5710211.html > Sent from the Mono for Android mailing list archive at Nabble.com. > _______________________________________________ > Monodroid mailing list > Monodroid@lists.ximian.com > > UNSUBSCRIBE INFORMATION: > http://lists.ximian.com/mailman/listinfo/monodroid > >
_______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid