[mono-android] Click event in Custom Adapter fire multiple positions

Petr Slováček pslovacek at newlink.cz
Thu Jun 7 09:47:53 UTC 2012


I had a similar problem when converting my Java app to Mono.

                check.Click += (sender, e) =>
                {
                   ...
                };

This code will add a new event listener each time GetView function is called. In C# event can have more than one listener assigned. In this case if you'd move both event assignments to "if (row == null)" block it should fire just once.

Also you should consider a better optimization for a ListView. It really makes a difference especially for more complex ListView data. You can find more info here: http://syndicatex.com/programming/monodroid-list-optimization/

Petr

-----Original Message-----
From: monodroid-bounces at lists.ximian.com [mailto:monodroid-bounces at lists.ximian.com] On Behalf Of Marcus
Sent: 7. června 2012 10:56
To: monodroid at lists.ximian.com
Subject: [mono-android] Click event in Custom Adapter fire multiple positions

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 at lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid


More information about the Monodroid mailing list