How to delete selected items in a ListView (Android.Resource.Layout.SimpleListItemMultipleChoice)?

KharFaiKharFai USMember
edited March 2017 in Xamarin.Android

I am using Visual Studio 2015 with Xamarin plugin. The Items of ListView are from SQLite Database Table class.
The link of UI image source : (https://us.v-cdn.net/5019960/uploads/editor/a4/x4b8h18kix7s.png)
Normal listview and i want to make the listview contain check boxes so that I can select some of the items in the listview to delete instead of just one item.
Did anyone know how to do this? I really can't figure it out how to make it. ='(
My Main Activity Code:
.....
ListView lstData;
List lstSource = new List();
DataBase db;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        //Create DataBase
        db = new DataBase();
        db.createDataBase();
        string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        Log.Info("DB_PATH", folder);

        lstData = FindViewById<ListView>(Resource.Id.listView);

        var edtName = FindViewById<EditText>(Resource.Id.edtName);
        var edtAge = FindViewById<EditText>(Resource.Id.edtAge);
        var edtEmail = FindViewById<EditText>(Resource.Id.edtEmail);

        var btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
        var btnEdit = FindViewById<Button>(Resource.Id.btnEdit);
        var btnDelete = FindViewById<Button>(Resource.Id.btnDelete);

        //LoadData
        LoadData();

        //Event
        btnAdd.Click += delegate
        {
            Person person = new Person() {
                Name = edtName.Text,
                Age = int.Parse(edtAge.Text),
                Email = edtEmail.Text
            };
            db.insertIntoTablePerson(person);
            LoadData();
        };

        btnEdit.Click += delegate {
            Person person = new Person()
            {
                Id=int.Parse(edtName.Tag.ToString()),
                Name = edtName.Text,
                Age = int.Parse(edtAge.Text),
                Email = edtEmail.Text
            };
            db.updateTablePerson(person);
            LoadData();
        };

        btnDelete.Click += delegate {
            Person person = new Person()
            {
                Id = int.Parse(edtName.Tag.ToString()),
                Name = edtName.Text,
                Age = int.Parse(edtAge.Text),
                Email = edtEmail.Text
            };
            db.deleteTablePerson(person);
            LoadData();
        };

        lstData.ItemClick += (s,e) =>{
            //Set background for selected item
            for(int i = 0; i < lstData.Count; i++)
            {
                if (e.Position == i)
                    lstData.GetChildAt(i).SetBackgroundColor(Android.Graphics.Color.DarkGray);
                else
                    lstData.GetChildAt(i).SetBackgroundColor(Android.Graphics.Color.Transparent);

            }

            //Binding Data
            var txtName = e.View.FindViewById<TextView>(Resource.Id.textView1);
            var txtAge = e.View.FindViewById<TextView>(Resource.Id.textView2);
            var txtEmail = e.View.FindViewById<TextView>(Resource.Id.textView3);

            edtName.Text = txtName.Text;
            edtName.Tag = e.Id;

            edtAge.Text = txtAge.Text;

            edtEmail.Text = txtEmail.Text;

        };

    }

    private void LoadData()
    {
        lstSource = db.selectTablePerson();
        var adapter = new ListViewAdapter(this, lstSource);
        lstData.Adapter = adapter;
    }
}

.....

Sign In or Register to comment.