public NoteItem GetItem(int id)
 {
     var t = new NoteItem ();
     lock (locker) {
         connection = new SqliteConnection ("Data Source=" + path);
         connection.Open ();
         using (var command = connection.CreateCommand ()) {
             command.CommandText = "SELECT [_id], [Name], [Description], [ImageUri], [Priority], [ToDoDate], [CreationDate] from [Items] WHERE [_id] = ?";
             command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = id });
             var r = command.ExecuteReader ();
             while (r.Read ()) {
                 t = FromReader (r);
                 break;
             }
         }
         connection.Close ();
     }
     return t;
 }
 /// <summary>Convert from DataReader to Task object</summary>
 NoteItem FromReader(SqliteDataReader r)
 {
     var t = new NoteItem ();
     t.ID = Convert.ToInt32 (r ["_id"]);
     t.Name = r ["Name"].ToString ();
     t.Description = r ["Description"].ToString ();
     t.ImageUri = r ["ImageUri"].ToString ();
     t.Priority = Convert.ToInt32(r ["Priority"]);
     t.ToDoDate = (DateTime)(r ["ToDoDate"]);
     t.CreationDate = (DateTime)(r ["CreationDate"]);
     return t;
 }
        public int SaveItem(NoteItem item)
        {
            int r;
            lock (locker) {
                if (item.ID != 0) {
                    connection = new SqliteConnection ("Data Source=" + path);
                    connection.Open ();
                    using (var command = connection.CreateCommand ()) {
                        command.CommandText = "UPDATE [Items] SET [Name] = ?, [Description] = ?, [ImageUri] = ?, [Priority] = ?, [ToDoDate] = ?, [CreationDate] = ? WHERE [_id] = ?;";
                        command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.Name });
                        command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.Description });
                        command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.ImageUri });
                        command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = item.Priority });
                        command.Parameters.Add (new SqliteParameter (DbType.DateTime) { Value = item.ToDoDate });
                        command.Parameters.Add (new SqliteParameter (DbType.DateTime) { Value = item.CreationDate });
                        command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = item.ID });
                        r = command.ExecuteNonQuery ();
                    }
                    connection.Close ();
                    return r;
                } else {
                    connection = new SqliteConnection ("Data Source=" + path);
                    connection.Open ();
                    using (var command = connection.CreateCommand ()) {
                        command.CommandText = "INSERT INTO [Items] ([Name], [Description], [ImageUri], [Priority], [ToDoDate], [CreationDate]) VALUES (? ,?, ?, ?, ?, ?)";
                        command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.Name });
                        command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.Description });
                        command.Parameters.Add (new SqliteParameter (DbType.String) { Value = item.ImageUri });
                        command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = item.Priority });
                        command.Parameters.Add (new SqliteParameter (DbType.DateTime) { Value = item.ToDoDate });
                        command.Parameters.Add (new SqliteParameter (DbType.DateTime) { Value = item.CreationDate });
                        r = command.ExecuteNonQuery ();
                    }
                    connection.Close ();
                    return r;
                }

            }
        }
 public static int SaveNote(NoteItem item)
 {
     return NoteItemRepository.SaveNote(item);
 }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate (bundle);

            //Find controls
            SetContentView(Resource.Layout.NoteEditScreen);
            nameTextEdit = FindViewById<EditText>(Resource.Id.NameText);
            descriptionTextEdit = FindViewById<EditText>(Resource.Id.NotesText);
            imagePreview = FindViewById<ImageView>(Resource.Id.NoteImage);
            saveButton = FindViewById<Button>(Resource.Id.SaveButton);
            pickImageButton = FindViewById<Button>(Resource.Id.PickImageButton);
            prioritySpinner = FindViewById<Spinner> (Resource.Id.PrioritySpinner);
            cancelDeleteButton = FindViewById<Button>(Resource.Id.CancelDeleteButton);
            dateDisplay = FindViewById<TextView> (Resource.Id.DateDisplay);
            timeDisplay = FindViewById<TextView> (Resource.Id.TimeDisplay);
            pickDate = FindViewById<Button> (Resource.Id.PickDate);
            pickTime = FindViewById<Button> (Resource.Id.PickTime);

            cancelDeleteButton.Text = Resources.GetText (Resource.String.Cancel);
            selectedDateTime = DateTime.Now;
            UpdateDisplay ();

            // Get note if exist
            int noteID = Intent.GetIntExtra("NoteID", 0);
            if(noteID > 0) {
                note = NoteItemManager.GetNote(noteID);

                nameTextEdit.Text = note.Name;
                descriptionTextEdit.Text = note.Description;
                prioritySpinner.SetSelection(note.Priority);

                cancelDeleteButton.Text = Resources.GetText (Resource.String.Delete);

                selectedDateTime = note.ToDoDate;
                selectedImageUri = note.ImageUri;
                selectedPriority = note.Priority;

                UpdateDisplay ();
            }

            //Image button
            pickImageButton.Click += (sender, eventArgs) =>
            {
                Intent = new Intent();
                Intent.SetType("image/*");
                Intent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(Intent, Resources.GetText (Resource.String.SelectPicture)), PickImageId);
            };

            //DateTime
            pickDate.Click += delegate { ShowDialog (PickDateId); };
            pickTime.Click += delegate { ShowDialog (PickTimeId); };

            //Priority spinner
            prioritySpinner.ItemSelected += PrioritySpinner_ItemSelected;
            var adapter = ArrayAdapter.CreateFromResource (
                this, Resource.Array.priorities, Android.Resource.Layout.SimpleSpinnerItem);
            adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
            prioritySpinner.Adapter = adapter;

            //Buttons clicks
            cancelDeleteButton.Click += (sender, e) => CancelDelete ();
            saveButton.Click += (sender, e) => Save ();
        }
 public static int SaveNote(NoteItem item)
 {
     return me.db.SaveItem(item);
 }