从listView项目执行函数/过程
抱歉,我正在使用Google Translate。
您好,我正在使用Visual Studio 2017在Xamarin Android开发一个应用程序,以获取订单,其中我将信息从片段发送到列表< en_wishdetalle>要存储项目的选择,并将其倾倒到适配器中以将其显示在另一个片段中。
在布局设计中,它具有3个按钮(添加数量/减少数量/删除项目),
当使用slist.removeat(position)从listView删除项目时,该错误在于; notifydatasetchanged();
这是在listView中删除和更新的,但是当我尝试再次执行上述任何按钮时,他们会拨打双调(好像他们被按两次,并且他们执行两次功能)
全部是notifyDataSetchanged();
我已经检查了它,但是,如果我用backspace(back)关闭片段并重新输入片段,并且一切正常工作,直到我再次使用删除项目按钮,并且我们回到同一问题;因此,我想避免使用它(notifydatasetchanged();),并寻找一种从适配器中重新加载片段的listView而没有任何成功的方法。我希望您的支持或建议走正确的道路。
适配器:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
try
{
if (row == null)
{
row = LayoutInflater.From(sContext).Inflate(Resource.Layout.item_Wish, null, false);
}
TextView txtCodigo = row.FindViewById<TextView>(Resource.Id.Codigo);
txtCodigo.Text = sList[position].codigo;
TextView txtArticulo = row.FindViewById<TextView>(Resource.Id.Articulo);
txtArticulo.Text = sList[position].articulo;
EditText txtCantidad = row.FindViewById<EditText>(Resource.Id.Cantidad);
txtCantidad.Text = sList[position].cantidad.ToString();
TextView txtPrecio = row.FindViewById<TextView>(Resource.Id.Precio);
txtPrecio.Text = sList[position].importetotal;
ImageView Art = row.FindViewById<ImageView>(Resource.Id.Image);
Art.SetImageResource(Android.Resource.Color.Transparent);
if (sList[position].imagenproducto == "")
{
Art.SetImageResource(Resource.Drawable.NoDisponible);
}
else
{
Android.Net.Uri myUri = (Android.Net.Uri.Parse(sList[position].imagenproducto));
//Art.SetImageURI(myUri);
Art.SetImageURI(myUri);
}
Button buttonMax = row.FindViewById<Button>(Resource.Id.btnMax);
Button buttonMin = row.FindViewById<Button>(Resource.Id.btnMin);
ImageButton buttonDel = row.FindViewById<ImageButton>(Resource.Id.btnDel);
buttonMax.Click += delegate
{
sList[position].cantidad = sList[position].cantidad + 1;
txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
};
buttonMin.Click += delegate
{
sList[position].cantidad = sList[position].cantidad - 1;
txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
};
buttonDel.Click += delegate
{
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(sContext);
AlertDialog alert = dialog.Create();
alert.SetTitle("GumisaAPP");
alert.SetMessage("Eliminar item : (" + position.ToString() + ") - " + sList[position].codigo + sList[position].articulo);
alert.SetIcon(Resource.Drawable.Alerta);
alert.SetButton("OK", (c, ev) =>
{
sList.RemoveAt(position);
NotifyDataSetChanged();
});
alert.SetButton2("CANCEL", (c, ev) =>
{
});
alert.Show();
//mAlertMessageBoxOk.onOkClick(5);
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally { }
return row;
}
片段:
private ListView WishlistView;
WishAdapter adapter;
List<EN_WishDetalle> List_Wish = new List<EN_WishDetalle>();
public override View OnCreateView(LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
//HasOptionsMenu = true;
var view = inflater.Inflate(Resource.Layout.Main_Wish, null);
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
EditText txtCantidad = view.FindViewById<EditText>(Resource.Id.Cantidad);
List_Wish = Variables.WishDetalle;
adapter = new WishAdapter(Activity, List_Wish);
//adapter2 = new WishAdapter( ).;
WishlistView.Adapter = adapter;
//InputSearch = view.FindViewById<EditText>(Resource.Id.inputSearch);
//InputSearch.TextChanged += InputSearch_TextChanged;
//List<EN_Clientes> objstud = new List<EN_Clientes>();
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
Button buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
Button buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
ImageButton buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
WishlistView.ItemClick += buttonMax_ItemClick;
//WishlistView.ItemClick += buttonMin_ItemClick;
//WishlistView.ItemClick += buttonDel_ItemClick;
return view;
}
void buttonMax_ItemClick(object sender,AdapterView.ItemClickEventArgs e)
{
}
void buttonMin_ItemClick(object sender, AdapterView.ItemClickEventArgs x)
{
}
void buttonDel_ItemClick(object sender, AdapterView.ItemClickEventArgs z)
{
}
还表明我有一个带有变量列表的类&lt; en_wishDetalle&gt;在公共场合能够将信息存储在内存中并避免使用数据库。
public static List<EN_WishDetalle> WishDetalle = new List<EN_WishDetalle>();
Sorry, I'm using google translate.
Hello, I am developing an app in xamarin android with visual studio 2017 for taking orders where I send information from a fragment to a List<EN_WishDetalle> to store the selection of items and this is dumped to an adapter to show it in another fragment.
In layout design it has 3 buttons (add quantity/decrease quantity/delete item),
the error lies when removing an item from the listview with sList.RemoveAt(position); NotifyDataSetChanged();
This is deleted and updated in the listview but when I try to execute any of the mentioned buttons again they make a double call (it is as if they were pressed twice and they execute their function twice)
all as a result of the NotifyDataSetChanged();
I have already checked it, however if I close the fragment with backspace (back) and re-enter everything works correctly until I use the delete item button again and we return to the same problem; so I thought of avoiding using it (NotifyDataSetChanged();) and looking for a way to reload the fragment's listview from the adapter without any success. I would like your support or suggestion to take the right path.
adapter:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
try
{
if (row == null)
{
row = LayoutInflater.From(sContext).Inflate(Resource.Layout.item_Wish, null, false);
}
TextView txtCodigo = row.FindViewById<TextView>(Resource.Id.Codigo);
txtCodigo.Text = sList[position].codigo;
TextView txtArticulo = row.FindViewById<TextView>(Resource.Id.Articulo);
txtArticulo.Text = sList[position].articulo;
EditText txtCantidad = row.FindViewById<EditText>(Resource.Id.Cantidad);
txtCantidad.Text = sList[position].cantidad.ToString();
TextView txtPrecio = row.FindViewById<TextView>(Resource.Id.Precio);
txtPrecio.Text = sList[position].importetotal;
ImageView Art = row.FindViewById<ImageView>(Resource.Id.Image);
Art.SetImageResource(Android.Resource.Color.Transparent);
if (sList[position].imagenproducto == "")
{
Art.SetImageResource(Resource.Drawable.NoDisponible);
}
else
{
Android.Net.Uri myUri = (Android.Net.Uri.Parse(sList[position].imagenproducto));
//Art.SetImageURI(myUri);
Art.SetImageURI(myUri);
}
Button buttonMax = row.FindViewById<Button>(Resource.Id.btnMax);
Button buttonMin = row.FindViewById<Button>(Resource.Id.btnMin);
ImageButton buttonDel = row.FindViewById<ImageButton>(Resource.Id.btnDel);
buttonMax.Click += delegate
{
sList[position].cantidad = sList[position].cantidad + 1;
txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
};
buttonMin.Click += delegate
{
sList[position].cantidad = sList[position].cantidad - 1;
txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
};
buttonDel.Click += delegate
{
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(sContext);
AlertDialog alert = dialog.Create();
alert.SetTitle("GumisaAPP");
alert.SetMessage("Eliminar item : (" + position.ToString() + ") - " + sList[position].codigo + sList[position].articulo);
alert.SetIcon(Resource.Drawable.Alerta);
alert.SetButton("OK", (c, ev) =>
{
sList.RemoveAt(position);
NotifyDataSetChanged();
});
alert.SetButton2("CANCEL", (c, ev) =>
{
});
alert.Show();
//mAlertMessageBoxOk.onOkClick(5);
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally { }
return row;
}
fragment:
private ListView WishlistView;
WishAdapter adapter;
List<EN_WishDetalle> List_Wish = new List<EN_WishDetalle>();
public override View OnCreateView(LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
//HasOptionsMenu = true;
var view = inflater.Inflate(Resource.Layout.Main_Wish, null);
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
EditText txtCantidad = view.FindViewById<EditText>(Resource.Id.Cantidad);
List_Wish = Variables.WishDetalle;
adapter = new WishAdapter(Activity, List_Wish);
//adapter2 = new WishAdapter( ).;
WishlistView.Adapter = adapter;
//InputSearch = view.FindViewById<EditText>(Resource.Id.inputSearch);
//InputSearch.TextChanged += InputSearch_TextChanged;
//List<EN_Clientes> objstud = new List<EN_Clientes>();
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
Button buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
Button buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
ImageButton buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
WishlistView.ItemClick += buttonMax_ItemClick;
//WishlistView.ItemClick += buttonMin_ItemClick;
//WishlistView.ItemClick += buttonDel_ItemClick;
return view;
}
void buttonMax_ItemClick(object sender,AdapterView.ItemClickEventArgs e)
{
}
void buttonMin_ItemClick(object sender, AdapterView.ItemClickEventArgs x)
{
}
void buttonDel_ItemClick(object sender, AdapterView.ItemClickEventArgs z)
{
}
Also indicate that I have a class with the variable List<EN_WishDetalle> in public to be able to store the information in memory and avoid using the database.
public static List<EN_WishDetalle> WishDetalle = new List<EN_WishDetalle>();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于ListView的回收机制,您可以使用
View> View -Code
和button标签
实现此目的。基于您的代码,我创建了一个简单的演示,它可以在我的身边进行
buttondel
。您可以参考以下代码:
Because of ListView's recycling mechanism, you can use
ViewHolder
and buttonTag
to achieve this.Based on your code, I created a simple demo, it works on my side for Button
buttonDel
.You can refer to the following code: