コード例 #1
0
        void buttonSave_Click(object sender, EventArgs e)
        {
            VehicleDbHelper db = new VehicleDbHelper(this);

            if (txtYear.Text.Trim().Length < 1)
            {
                Toast.MakeText(this, "Enter Year.", ToastLength.Short).Show();
                return;
            }

            if (txtMake.Text.Trim().Length < 1)
            {
                Toast.MakeText(this, "Enter Make.", ToastLength.Short).Show();
                return;
            }

            if (txtModel.Text.Trim().Length < 1)
            {
                Toast.MakeText(this, "Enter Model.", ToastLength.Short).Show();
                return;
            }

            if (txtColor.Text.Trim().Length < 1)
            {
                Toast.MakeText(this, "Enter Color.", ToastLength.Short).Show();
                return;
            }

            Vehicle ab = new Vehicle();

            if (txtId.Text.Trim().Length > 0)
            {
                ab.Id = int.Parse(txtId.Text);
            }
            ab.Year        = txtYear.Text;
            ab.Make        = txtMake.Text;
            ab.Model       = txtModel.Text;
            ab.Color       = txtColor.Text;
            ab.Description = txtDescription.Text;

            if (sbPaint.Progress > 0)
            {
                ab.PercentagePaint = sbPaint.Progress;
            }
            //Body
            if (sbBody.Progress > 0)
            {
                ab.PercentageBody = sbBody.Progress;
            }
            //Glass
            if (sbGlass.Progress > 0)
            {
                ab.PercentageGlass = sbGlass.Progress;
            }
            //Trim
            if (sbTrim.Progress > 0)
            {
                ab.PercentageTrim = sbTrim.Progress;
            }
            //Upholstery
            if (sbUpholstery.Progress > 0)
            {
                ab.PercentageUpholstery = sbUpholstery.Progress;
            }
            //Mechanical
            if (sbMechanical.Progress > 0)
            {
                ab.PercentageMechanical = sbMechanical.Progress;
            }
            //Electrical
            if (sbMechanical.Progress > 0)
            {
                ab.PercentageElectrical = sbMechanical.Progress;
            }
            //Frame
            if (sbFrame.Progress > 0)
            {
                ab.PercentageFrame = sbFrame.Progress;
            }
            //Tires
            if (sbTires.Progress > 0)
            {
                ab.PercentageTires = sbTires.Progress;
            }
            if (sbOverall.Progress > 0)
            {
                ab.PercentageOverall = sbOverall.Progress;
            }


            try
            {
                if (txtId.Text.Trim().Length > 0)
                {
                    db.UpdateAuto(ab);
                    Toast.MakeText(this, "Auto Updated Successfully.", ToastLength.Short).Show();
                }
                else
                {
                    db.AddNewAuto(ab);
                    Toast.MakeText(this, "New Auto Created Successfully.", ToastLength.Short).Show();
                }

                Finish();

                //Go to main activity after save/edit
                var mainActivity = new Intent(this, typeof(MainActivity));
                StartActivity(mainActivity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }