我使用适配器视图和内部适配器按钮单击事件来打开另一个activityB。当我在我的活动B中按下后退按钮时,它会重新打开活动B 7次,然后它会返回到我之前存在我的适配器ViewA的ActivityA。就像它在适配器视图中运行我的按钮单击事件一样当我按下返回按钮时自动。
这是我的适配器代码:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
row = LayoutInflater.From(mContext).Inflate(Resource.Layout.InventoryPreview, null, false);
}
TextView txtInventoryID = row.FindViewById<TextView>(Resource.Id.txtInventoryID);
txtInventoryID.Text = mitems[position].InventoryItemID;
TextView txtInventoryName = row.FindViewById<TextView>(Resource.Id.txtInventoryName);
txtInventoryName.Text = mitems[position].InventoryItemName;
TextView txtInventoryPrice = row.FindViewById<TextView>(Resource.Id.txtInventoryPrice);
txtInventoryPrice.Text = mitems[position].InventoryItemPrice.Replace(",", ".");
Button ExtraBtn = row.FindViewById<Button>(Resource.Id.ExtrasBtn);
ExtraBtn.Click += (sender, e) =>
{
try
{
mContext.StartActivity(typeof(ExtrasPreviewMain));//Here I open ActivityB.
}
catch (Exception ex)
{
Toast toast = Toast.MakeText(mContext, Convert.ToString(ex), ToastLength.Long);
toast.Show();
}
};
return row;
}
这是我的活动B Back Press:
public override void OnBackPressed()
{
base.OnBackPressed();
OverridePendingTransition(Resource.Layout.trans_right_in, Resource.Layout.trans_right_out);
}
此外,我已在我的XML文件中禁用了自动对焦功能。为什么会这样?
答案 0 :(得分:0)
此处的问题是,每次调用GetView
时,都会分配按钮Clicked
事件的事件处理程序。你似乎永远不会分配它。
因此,解决此问题的一种方法是仅在row == null
时分配点击处理程序。
另一种方法是没有按钮并在ListView上使用ItemSelected
事件。