How to delete from Sqlite-net and listview at the same time through button click?

DarkArTDarkArT ROMember

Hello,

I have a Custom Adapter and the list-view that list the item from database.
My Question is. How can i remove the row from Database trough a selected item in list-view when i click on delete button or on the item?

My CustomAdapter:

    public class CustomAdapter : BaseAdapter
    {
        private TextView txtName;
        private TextView txtAge;
        private TextView txtEmail;

        private Activity _activity;
        private List<Person> _list;

        public CustomAdapter(Activity activity, List<Person> list)
        {
            _activity = activity;
            _list = list;
        }

        public override int Count
        {
            get { return _list.Count; }
        }

        public override Object GetItem(int position)
        {
            return null;
        }

        public override long GetItemId(int position)
        {
            return _list[position].id;
        }

        public override View GetView(int position, View converView, ViewGroup parent)
        {
            var view = converView ??
                       _activity.LayoutInflater.Inflate(Resource.Layout.lst_view_dataTemplate, parent, false);

            txtName = view.FindViewById<TextView>(Resource.Id.textView1);
            txtAge = view.FindViewById<TextView>(Resource.Id.textView2);
            txtEmail = view.FindViewById<TextView>(Resource.Id.textView3);

            txtName.Text = _list[position].Name;
            txtAge.Text = _list[position].Age;
            txtEmail.Text = _list[position].Email;

            return view;
        }

    }

My MainActivity:

    public class MainActivity : Activity
    {
        private EditText txt1;
        private EditText txt2;
        private Button btn2;
        private ListView lst_View;
        private List<Person> lst_Source;
        private PersonDb tb1;
        private Person person;
        private string dpPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "lst.db3");

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

            lst_View = FindViewById<ListView>(Resource.Id.listView1);
            txt1 = FindViewById<EditText>(Resource.Id.editText1);
            txt2 = FindViewById<EditText>(Resource.Id.editText2);
            btn2 = FindViewById<Button>(Resource.Id.button2);

            TestList();

            lst_View.ItemLongClick += longclickevent;

            btn2.Click += delegate
            {
                try
                {
                    var db = new SQLiteConnection(dpPath);
                    db.CreateTable<PersonDb>();
                    tb1 = new PersonDb();
                    tb1.Name = txt1.Text;
                    tb1.Age = txt2.Text;
                    db.Insert(tb1);

                    var tb = db.Table<PersonDb>();
                    lst_Source = new List<Person>();
                    foreach (var tblist in tb)
                    {
                        Person person = new Person()
                        {
                            id = tblist.id,
                            Name = tblist.Name,
                            Age = tblist.Age,
                            Email = tblist.Email
                        };
                        lst_Source.Add(person);
                    }
                    var adapter = new CustomAdapter(this, lst_Source);
                    lst_View.Adapter = adapter;
                    adapter.NotifyDataSetChanged();
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
                }
            };

            CreateDatabase();
        }

        private void longclickevent(object sender, AdapterView.ItemLongClickEventArgs e)
        {

        }

        private void TestList()
        {
            var db = new SQLiteConnection(dpPath);
            var tb = db.Table<PersonDb>();
            lst_Source = new List<Person>();
            foreach (var tblist in tb)
            {
                person = new Person() { id = tblist.id, Name = tblist.Name, Age = tblist.Age, Email = tblist.Email };
                lst_Source.Add(person);
            }
            var adapter = new CustomAdapter(this, lst_Source);
            lst_View.Adapter = adapter;
            adapter.NotifyDataSetChanged();
        }

        private void CreateDatabase()
        {
            try
            {
                var db = new SQLiteConnection(dpPath);
                db.CreateTable<PersonDb>();
                Toast.MakeText(this, "Database created", ToastLength.Short).Show();
            }
            catch (SQLiteException ex)
            {
                Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
            }
        }

    }

Sorry if my Code is too long.

Thank you.

Answers

Sign In or Register to comment.