从listView项目执行函数/过程

发布于 2025-01-25 19:36:57 字数 5560 浏览 2 评论 0原文

抱歉,我正在使用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>();

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2025-02-01 19:36:57

由于ListView的回收机制,您可以使用View> View -Code和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; }

    }

Because of ListView's recycling mechanism, you can use ViewHolder and button Tag 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:

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; }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文