Xamarin Android 记事本(三)删改

这篇我就不做太多的说明了,数据操作之前也都举过例子了,这里就直接贴出删除和修改的代码。

 public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case 1:
                    {
                        Android.App.AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        AlertDialog ad = builder.Create();
                        ad.SetTitle("是否删除");                    
                        ad.SetButton("", (s, e) =>
                        {
                            Sqlite dbHelper = new Sqlite(this);
                            SQLiteDatabase db = dbHelper.WritableDatabase;
                            ContentValues ncv = new ContentValues();
                            var NID = Intent.GetStringExtra("id");
                            ncv.Put("context", edtext.Text);
                            db.Delete("NoteBooksql", " _id = ? ", new string[] { NID.ToString() });
                            var intentAct2 = new Intent(this, typeof(Activity1));
                            StartActivity(intentAct2);
                            Toast.MakeText(this, "已删除", ToastLength.Short).Show();
                            this.Finish();
                           
                        });
                        ad.SetButton2("", (s, e) => { });
                        ad.Show();
                        
                    } break;
                case 2:
                     {
                         Sqlite dbHelper = new Sqlite(this);
                         SQLiteDatabase db = dbHelper.WritableDatabase;
                         ContentValues ncv = new ContentValues();
                         var NID = Intent.GetStringExtra("id");
                         ncv.Put("context", edtext.Text);
                         db.Update("NoteBooksql", ncv, " _id = ? ", new string[] { NID.ToString() });
                         var intentAct2 = new Intent(this, typeof(Activity1));
                         StartActivity(intentAct2);
                         Toast.MakeText(this, "已保存", ToastLength.Short).Show();
                         this.Finish();
                     }break;
                case Android.Resource.Id.Home:
                    {
                        var intent = new Intent(this, typeof(Activity1));
                        StartActivity(intent);
                        this.Finish();
                    } break;
            }
            return true;
        }

效果图如下

posted on 2015-03-14 08:24  立华奏  阅读(472)  评论(1编辑  收藏  举报