ListView and Delete button

Pleaaaaaaaaseeeeee! Give an simple example of a listView with the button delete rows.

My code works strange
    class myAdapter:BaseAdapter<TableItem>
        {

        List<TableItem> items;
        Activity context;
        public myAdapter(Activity context, List<TableItem> items)
           : base()
       {
            this.context = context;
            this.items = items;
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public override TableItem this[int position]
        {
            get { return items[position]; }
        }
        public override int Count
        {
            get { return items.Count; }
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];
            View view = convertView;
            Button btnDelete;

            if (view == null)
            {
                view = context.LayoutInflater.Inflate(Resource.Layout.item_view, null);
            }
                view.FindViewById<TextView>(Resource.Id.textView1).Text = item.title;        

                btnDelete = view.FindViewById<Button>(Resource.Id.button2);

                btnDelete.Click += (sender, args) =>
                {
                    items.RemoveAt(position);                   
                    NotifyDataSetChanged();
                };

            return view;
        }
    }


namespace customAdapter
{
    [Activity(Label = "customAdapter", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

        ListView listok;
        Button btn;
        List<TableItem> items = new List<TableItem>();
        myAdapter ad;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            listok = FindViewById<ListView>(Resource.Id.listView1);
            btn = FindViewById<Button>(Resource.Id.button1);

            items.Add(new TableItem("dfdfd", 1));
            items.Add(new TableItem("ertre2", 2));
            items.Add(new TableItem("sdfds3", 3));
            items.Add(new TableItem("sdfds4", 4));
            items.Add(new TableItem("sdfds5", 5));
            items.Add(new TableItem("sdfds6", 6));

            ad = new myAdapter(this, items);
            listok.Adapter = ad;
        }       
    }   
}

Answers

  • AziziAzizAziziAziz USMember ✭✭

    Hi @DmitriyAkenfyev ,

    I'm not sure what the problem is. Anyway, in your myAdapter class, try removing the if (view == null).

    You just inflate the view directly without have to check if the view is null.

    Hope this helps.
    AziziAziz

  • @AziziAziz said:
    Hi @DmitriyAkenfyev ,

    I'm not sure what the problem is. Anyway, in your myAdapter class, try removing the if (view == null).

    You just inflate the view directly without have to check if the view is null.

    Hope this helps.
    AziziAziz

    Hi, thank! But, no.

    It was necessary to remove and add delegate and set tag to button.

    Valid code:

    public override View GetView(int position, View convertView, ViewGroup parent)
            {
                var item = items[position];
                View view = convertView;
                Button btnDelete;
    
                if (view == null)
                {
                    view = context.LayoutInflater.Inflate(Resource.Layout.item_view, null);
                }  
                view.FindViewById<TextView>(Resource.Id.textView1).Text = item.title;        
    
                btnDelete = view.FindViewById<Button>(Resource.Id.button2);
    
                btnDelete.SetTag(Resource.Id.button2, position);
    
                btnDelete.Click -= MyClickEvent;
                btnDelete.Click += MyClickEvent;
    
                return view;
            }
    
            void MyClickEvent(object sender, EventArgs e)
            {
                int i = (int)((Button)sender).GetTag(Resource.Id.button2);
                items.RemoveAt(i);
                NotifyDataSetChanged();
            }
    
Sign In or Register to comment.