Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had used listview with custom listview adapter and i had generated a click event for imageview control of custom adapter. Now the issue is my click event fires multiple time with different position.
Here is some code which i had tried so far :-

<pre lang="C#">
public override View GetView(int position, View convertView, ViewGroup parent)
		{
			try {
				ProductViewHolder holder = null;
				RetriveState();

				if(convertView == null)
				{
					holder = new ProductViewHolder();
					convertView = context.LayoutInflater.Inflate(Resource.Layout.ProductListTabView,parent,false);
					holder.ivPlus = convertView.FindViewById<ImageView> (Resource.Id.ivPlus); 
					convertView.Tag = holder;
				}
				else 
					holder = (ProductViewHolder)convertView.Tag;

				var item = itemsList.ElementAt(position);
			  
				//this is where i am facing the issue coz it fires multiple times
				holder.ivPlus.Click +=  delegate  {
					ProgressDialog progressDialog = ProgressDialog.Show (context, "", "Loading Data.."); 
					int currentQty = item.Quantity; //Convert.ToInt32(holder.tvQty.Text);
					holder.tvQty.Text = (currentQty + 1).ToString(); 
					itemsList.ElementAt(position).Quantity = Convert.ToInt32(holder.tvQty.Text);
					progressDialog.Dismiss(); 
				};
			}
		}


Can anyone tell me what i am doing wrong over here?
Posted
Updated 17-Apr-17 23:20pm

1 solution

Hello,

I had a similar issue and fixed it by using the ViewHolder-pattern. You seem to already be using it so I would like to show you how I would initialize it.
C#
public override View GetView(int position, View convertView, ViewGroup parent)
{
    ProductViewHolder holder = null;
    RetrieveState();

    if (convertView == null)
        holder = view.Tag as ProductViewHolder;
    if (holder == null)
    {
        holder = new ProductViewHolder();
        convertView = context.LayoutInflater.Inflate(
                Resource.Layout.ProductListTabView,parent,false);
        ...
        holder.Tag = holder;
    }
}

This way the holder is not re-initialized multiple times even if convertView were to change to null at a later time. The holder works kind of like a Singleton if you are familiar with that pattern, since it is guaranteed to only be created once. And therefore the statement
C#
holder.ivPlus.Click += delegate {... }

will only get called once.

In C# you add a function pointer (delegate) to an event handler by using the += operator. In your case, you add a new function pointer to the handler to call every time convertView is null, leading to it seeming like multiple click events were fired.
There is no way to assign only one function pointer to an event handler, it works like a list to which you can either add with += or remove with -=.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900