private void OnClick(object sender, EventArgs eventArgs) { var newVehicle = new VehicleDecorator { Name = FindViewById<EditText>(Resource.Id.vehicleName).Text.Trim() }; var svc = new MyTripService(this); if (svc.Save(newVehicle)) { Log.Verbose(TAG, "Added the new vehicle."); } SetResult(Result.Ok); Finish(); }
/// <summary> /// Save the vehicle to the database. Don't allow for duplicates. /// </summary> /// <param name="vehicle"> </param> /// <returns> </returns> public bool Save(VehicleDecorator vehicle) { Toast toast; var didSave = false; if (vehicle.IsValid()) { Log.Verbose(TAG, "The vehicle was valid - saving to the database."); if (DoesVehicleNameExist(vehicle.Name)) { Log.Verbose(TAG, "The vehicle already exists in the database - nothing was done."); return true; } using (var db = _dbHelper.WritableDatabase) { try { db.InsertOrThrow(VehicleTable.TABLE_NAME, null, vehicle.ToContentValues()); Log.Verbose(TAG, "Saved a new vehicle to the database."); toast = Toast.MakeText(_context, "Saved your vehicle.", ToastLength.Short); didSave = true; } catch (Exception ex) { Log.Error(TAG, ex.ToString()); toast = Toast.MakeText(_context, "There was an error trying to save your vehicle. Sorry.", ToastLength.Long); } } } else { Log.Debug(TAG, "The vehicle is not valid."); toast = Toast.MakeText(_context, "There is something wrong with your data. You should fix it.", ToastLength.Long); } toast.Show(); return didSave; }