从 Listview 项目 post 执行 Function/Procedure - 在 xamarin android 中删除适配器
Execute Function/Procedure from Listview item post-delete adapter in xamarin android
抱歉,我正在使用 google 翻译。
你好,我正在用 visual studio 2017 在 xamarin android 中开发一个应用程序,用于接收订单,我将信息从片段发送到列表 以存储选择项目,这被转储到适配器以在另一个片段中显示它。
在布局设计中它有 3 个按钮(添加 quantity/decrease quantity/delete 项),
使用 sList.RemoveAt(position); NotifyDataSetChanged();
从列表视图中删除项目时出现错误
这在列表视图中被删除和更新但是当我尝试再次执行任何提到的按钮时,它们会进行两次调用(就好像它们被按下两次并且它们执行它们的功能两次)
都是 NotifyDataSetChanged();
的结果
我已经检查过了,但是如果我用退格键关闭片段并重新输入一切正常,直到我再次使用删除项目按钮并且我们 return 遇到同样的问题;所以我想避免使用它 (NotifyDataSetChanged();) 并寻找一种方法从适配器重新加载片段的列表视图但没有成功。我希望您的支持或建议走正确的道路。
适配器:
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)
{
}
另外说明我在public中有一个class,变量List可以将信息存储在内存中,避免使用数据库。
public static List<EN_WishDetalle> WishDetalle = new List<EN_WishDetalle>();
enter image description here
enter image description here
由于ListView的回收机制,可以使用ViewHolder
和按钮Tag
来实现。
根据您的代码,我创建了一个简单的演示,它适用于我这边的 Button buttonDel
。
您可以参考以下代码:
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView; // re-use an existing view, if one is available
MyViewHolder holder;
if (view != null)
{
holder = view.Tag as MyViewHolder;
holder.buttonDel.Tag = position;
//holder.buttonMax.Tag = position;
//holder.buttonMin.Tag = position;
}
else
{ // otherwise create a new one
holder = new MyViewHolder();
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = inflater.Inflate(Resource.Layout.CustomView, null);
holder.HeadingTextView = view.FindViewById<TextView>(Resource.Id.Text1);
holder.SubHeadingTextView = view.FindViewById<TextView>(Resource.Id.Text2);
holder.IconImage = view.FindViewById<ImageView>(Resource.Id.Image);
holder.buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
holder.buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
holder.buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
holder.buttonMax.Click += delegate
{
};
holder.buttonMin.Click += delegate
{
};
holder.buttonDel.Click += delegate
{ // we get the tag here for buttonDel
int position = (int)holder.buttonDel.Tag;
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(context);
AlertDialog alert = dialog.Create();
alert.SetTitle("GumisaAPP");
alert.SetMessage("Eliminar item : (" + position.ToString() + ") - " + items[position].Heading + items[position].SubHeading);
alert.SetIcon(Resource.Drawable.love);
alert.SetButton("OK", (c, ev) =>
{
items.RemoveAt(position);
NotifyDataSetChanged();
});
alert.SetButton2("CANCEL", (c, ev) =>
{
});
alert.Show();
};
holder.buttonDel.Tag = position;
view.Tag = holder;
}
holder.HeadingTextView.Text = item.Heading;
holder.SubHeadingTextView.Text = item.SubHeading;
holder.IconImage.SetImageResource(item.ImageResourceId);
return view;
}
public class MyViewHolder : Java.Lang.Object
{
public TextView HeadingTextView { get; set; }
public TextView SubHeadingTextView { get; set; }
public ImageView IconImage { get; set; }
public Button buttonMax { get; set; }
public Button buttonMin { get; set; }
public ImageButton buttonDel { get; set; }
}
抱歉,我正在使用 google 翻译。
你好,我正在用 visual studio 2017 在 xamarin android 中开发一个应用程序,用于接收订单,我将信息从片段发送到列表
在布局设计中它有 3 个按钮(添加 quantity/decrease quantity/delete 项),
使用 sList.RemoveAt(position); NotifyDataSetChanged();
从列表视图中删除项目时出现错误
这在列表视图中被删除和更新但是当我尝试再次执行任何提到的按钮时,它们会进行两次调用(就好像它们被按下两次并且它们执行它们的功能两次)
都是 NotifyDataSetChanged();
我已经检查过了,但是如果我用退格键关闭片段并重新输入一切正常,直到我再次使用删除项目按钮并且我们 return 遇到同样的问题;所以我想避免使用它 (NotifyDataSetChanged();) 并寻找一种方法从适配器重新加载片段的列表视图但没有成功。我希望您的支持或建议走正确的道路。
适配器:
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)
{
}
另外说明我在public中有一个class,变量List
public static List<EN_WishDetalle> WishDetalle = new List<EN_WishDetalle>();
enter image description here
enter image description here
由于ListView的回收机制,可以使用ViewHolder
和按钮Tag
来实现。
根据您的代码,我创建了一个简单的演示,它适用于我这边的 Button buttonDel
。
您可以参考以下代码:
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView; // re-use an existing view, if one is available
MyViewHolder holder;
if (view != null)
{
holder = view.Tag as MyViewHolder;
holder.buttonDel.Tag = position;
//holder.buttonMax.Tag = position;
//holder.buttonMin.Tag = position;
}
else
{ // otherwise create a new one
holder = new MyViewHolder();
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = inflater.Inflate(Resource.Layout.CustomView, null);
holder.HeadingTextView = view.FindViewById<TextView>(Resource.Id.Text1);
holder.SubHeadingTextView = view.FindViewById<TextView>(Resource.Id.Text2);
holder.IconImage = view.FindViewById<ImageView>(Resource.Id.Image);
holder.buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
holder.buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
holder.buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
holder.buttonMax.Click += delegate
{
};
holder.buttonMin.Click += delegate
{
};
holder.buttonDel.Click += delegate
{ // we get the tag here for buttonDel
int position = (int)holder.buttonDel.Tag;
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(context);
AlertDialog alert = dialog.Create();
alert.SetTitle("GumisaAPP");
alert.SetMessage("Eliminar item : (" + position.ToString() + ") - " + items[position].Heading + items[position].SubHeading);
alert.SetIcon(Resource.Drawable.love);
alert.SetButton("OK", (c, ev) =>
{
items.RemoveAt(position);
NotifyDataSetChanged();
});
alert.SetButton2("CANCEL", (c, ev) =>
{
});
alert.Show();
};
holder.buttonDel.Tag = position;
view.Tag = holder;
}
holder.HeadingTextView.Text = item.Heading;
holder.SubHeadingTextView.Text = item.SubHeading;
holder.IconImage.SetImageResource(item.ImageResourceId);
return view;
}
public class MyViewHolder : Java.Lang.Object
{
public TextView HeadingTextView { get; set; }
public TextView SubHeadingTextView { get; set; }
public ImageView IconImage { get; set; }
public Button buttonMax { get; set; }
public Button buttonMin { get; set; }
public ImageButton buttonDel { get; set; }
}