问题
I want click method to be called only once, what can I do get this result? here's my code:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View result = convertView;
if (result == null)
{
result = LayoutInflater.From(_Context).Inflate(Resource.Layout.DiagnoseTemplateLayout, null, false);
}
TextView textView = result.FindViewById<TextView>(Resource.Id.DiagnoseTemplateTextView);
textView.Text = _Diagnoses[position].Description;
Button button = result.FindViewById<Button>(Resource.Id.DeleteDiagnoseTemplateButton);
button.Tag = _Diagnoses[position].Key;
button.Click += delegate
{
if (_Diagnoses[position] != null)
{
AlertDialog.Builder builder = new AlertDialog.Builder(_Context);
builder.SetTitle("Varning");
builder.SetMessage("Delating selected item?");
builder.SetPositiveButton("YES", delegate
{
SuperBillAddActivity.SuperBill.Diagnoses.Remove(_Diagnoses[position]);
SuperBillAddActivity.RefreshLists();
});
builder.SetNegativeButton("NO", delegate { });
builder.Show();
}
};
return result;
}
This method is called 3 times button.Click += delegate starting from position from where is button we clicked
public override View GetView(int position, View convertView, ViewGroup parent)
{
View result = convertView;
if (result == null)
{
result = LayoutInflater.From(_Context).Inflate(Resource.Layout.DiagnoseTemplateLayout, null, false);
}
TextView textView = result.FindViewById<TextView>(Resource.Id.DiagnoseTemplateTextView);
textView.Text = _Diagnoses[position].Description;
Button button = result.FindViewById<Button>(Resource.Id.DeleteDiagnoseTemplateButton);
button.Tag = _Diagnoses[position].Key;
button.Click += delegate
{
if ((_Clicked) && (_Diagnoses[position] != null))
{
_Clicked = !_Clicked;
AlertDialog.Builder builder = new AlertDialog.Builder(_Context);
builder.SetTitle("Varning");
builder.SetMessage("Delating selected item?");
builder.SetPositiveButton("YES", delegate
{
_Clicked = !_Clicked;
SuperBillAddActivity.SuperBill.Diagnoses.Remove(_Diagnoses[position]);
SuperBillAddActivity.RefreshLists();
});
builder.SetNegativeButton("NO", delegate
{
_Clicked = !_Clicked;
});
builder.Show();
}
};
return result;
}
_Clicked <- I implemented this and now the problem is solved but I don't like this way of putting a code. is there an alternative to this? also, i'm interested why first code worked the way it did.
回答1:
GetView
is called multiple times, in consequence your doing button.Click += delegate
multiples times too. And this is the reason why your getting too much calls in Click
.
Try:
Button button;
if (result == null)
{
result = LayoutInflater.From(_Context).Inflate(Resource.Layout.DiagnoseTemplateLayout, null, false);
button = result.FindViewById<Button>(Resource.Id.DeleteDiagnoseTemplateButton);
button.Click += delegate {...}
}
TextView textView = result.FindViewById<TextView>(Resource.Id.DiagnoseTemplateTextView);
textView.Text = _Diagnoses[position].Description;
button.Tag = _Diagnoses[position].Key;
来源:https://stackoverflow.com/questions/38263854/click-method-inside-getview-called-multiple-times