Subramanian
Subramanian

Reputation: 373

Custom ListView alert box not closing

I had create custom listview in C#, it is working fine. The problem I am facing is, the list contains more than 50 records. For a deleting a record I am using a delete button. When I click delete button it opens alert box asking for confirmation. All these things are working fine upto first 10 records in the list, when I scroll the list to the bottom, and click the delete button there only problem starts, in the alert box, when I press the OK button it is not closing, instead it keeps moving to other list items. After 2 or 3 clicks then only the alert box gets closed, is there any way to solve this problem

using System.Collections.Generic;
using Android.App;
using Android.Views;
using Android.Widget;
using MyAndroid.Models;

namespace MyAndroid.Adapter
{
 class UserListAdapter : BaseAdapter<User>
  {
    private Activity context;
    private List<User> users;
    public override int Count
    {
        get
        {
            return users.Count;
        }
    }

    public override User this[int position]
    {
        get
        {
            return users[position];
        }
    }

    public UserListAdapter(Activity context, List<User> users)
    {
        this.context = context;
        this.users = users;
    }

    public override long GetItemId(int position)
    {
        return position;
    }


    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView;
        if (view == null) {
            view = context.LayoutInflater.Inflate(Resource.Layout.userlayout, parent, false);
            var name = view.FindViewById<TextView>(Resource.Id.nameTextView);
            var deleteUser = view.FindViewById<ImageView>(Resource.Id.userDelete);
            var addUser = view.FindViewById<ImageView>(Resource.Id.userAdd);
            view.Tag = new ViewHolder() { Name = name, btnDelete = deleteUser, btnAdd = addUser };
        }
        var holder = (ViewHolder)view.Tag;
        holder.id = users[position].Id;
        holder.Name.Text = users[position].LastName;
        holder.btnDelete.Click += (sender, args) =>
        {
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            AlertDialog alert = dialog.Create();
            alert.SetTitle("Delete");
            alert.SetMessage("Are you sure you want to delete this user");



            alert.SetButton("OK", (c, ev) =>
            {
                Toast.MakeText(context, "You going to delete " + holder.id + " " + users[position].FirstName + " " + users[position].LastName, ToastLength.Long).Show();
                alert.Dismiss();
                alert.Hide();
            });
            alert.SetCancelable(true);
            alert.Show();

        };
        return view;
    }
}

public class ViewHolder : Java.Lang.Object
{
    public int id { get; set; }
    public TextView Name { get; set; }

    public ImageView btnDelete { get; set; }

    public ImageView btnAdd { get; set; }
}

}

This is the debug log I am getting

I/chatty  (31411): uid=10087(com.companyname.myandroid) RenderThread identical 
7 lines
01-22 10:16:30.056 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:31.431 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:31.688 I/chatty  (31411): uid=10087(com.companyname.myandroid) 
RenderThread identical 34 lines
01-22 10:16:31.690 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:31.730 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:31.784 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:31.787 D/OpenGLRenderer(31411): endAllActiveAnimators on 
0xc0ea1000 (RippleDrawable) with handle 0xc48bc0d0
01-22 10:16:31.857 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:32.678 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:32.733 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:32.738 D/OpenGLRenderer(31411): endAllActiveAnimators on 
0xc12bd900 (RippleDrawable) with handle 0xc48bc0c0
01-22 10:16:32.819 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:33.802 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:33.825 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:33.828 D/OpenGLRenderer(31411): endAllActiveAnimators on 
0xc12bd200 (RippleDrawable) with handle 0xc232df60
01-22 10:16:33.908 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)
01-22 10:16:33.916 D/EGL_emulation(31411): eglMakeCurrent: 0xe03055a0: ver 3 0 
(tinfo 0xc59d65e0)

Upvotes: 0

Views: 71

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14981

you could change your GetView() like this:

public override View GetView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder = null;
    var view = convertView;
    if (view == null) {
        view = context.LayoutInflater.Inflate(Resource.Layout.userlayout, parent, false);
        var name = view.FindViewById<TextView>(Resource.Id.nameTextView);
        var deleteUser = view.FindViewById<ImageView>(Resource.Id.userDelete);
        var addUser = view.FindViewById<ImageView>(Resource.Id.userAdd);
        holder = new ViewHolder() { Name = name, btnDelete = deleteUser, btnAdd = addUser };
        view.Tag = holder; 
        holder.btnDelete.Click += (sender, args) =>
        {
          int btnPosition = (int)((Button)sender).Tag;
          AlertDialog.Builder dialog = new AlertDialog.Builder(context);
          AlertDialog alert = dialog.Create();
          alert.SetTitle("Delete");
          alert.SetMessage("Are you sure you want to delete this user");
          alert.SetButton("OK", (c, ev) =>
          {
            Toast.MakeText(context, "You going to delete " + holder.id + " " + users[btnPosition].FirstName + " " + users[btnPosition].LastName, ToastLength.Long).Show();
            alert.Dismiss();
            alert.Hide();
          });
          alert.SetCancelable(true);
          alert.Show();

         };
     }
     holder = (ViewHolder)view.Tag;
     holder.btnDelete.Tag = position;
     holder.id = users[position].Id;
     holder.Name.Text = users[position].LastName;
     return view;
}

Upvotes: 1

Related Questions