Monday, July 18, 2016

Inserting first Records Mileage App

Setting up the Activities


I am not going to show all the code for the Android application, only the parts relevant to the discussion of SQLite. When I am done I will post all the code on github, and place a link here.

Suffice it to say for now, I created two Activities: the main one to Enter mileage and a second one to Enter a vehicle. I used a menu to navigate between the two.

main activity

menu

Coding the Inserts

It was necessary to enter the vehicle before I could enter mileage, so I wrote the insert code for the vehicle first. I used an anonymous class for the button listener. Inside it, first I got the values from the EditText controls:

   carMakeText=(EditText)findViewById(R.id.editTextMake);
                String make =carMakeText.getText().toString();

                carModelText=(EditText)findViewById(R.id.editTextModel);
                String model =carModelText.getText().toString();

                carYear = (EditText)findViewById(R.id.editTextYear);
                int year = Integer.parseInt(carYear.getText().toString());

Then I declared an instance of my dataHelper class:

SqlHelper helper = new SqlHelper(Main2Activity.this);
                SQLiteDatabase db= helper.getWritableDatabase();

Then I created a collection of values and matched the values from the EditText controls with the underlying table field.

ContentValues values = new ContentValues();
                values.put(helper.VEHICLE_MAKE,make);
                values.put(helper.VEHICLE_MODEL, model);
                values.put(helper.VEHICLE_YEAR, year);
!--end code beautifier-->

Then I insert the data and close the database.

 long car_Id=db.insert(helper.VEHICLE_TABLE, null, values);

                db.close();

Finally I use an if then with Toast to test whether the insert worked or not.

  if(car_Id != -1)
                {
                    //start a toast (a message)
                    Toast toast = Toast.makeText
                            (Main2Activity.this, "Record Added " + car_Id, Toast.LENGTH_LONG);
                    toast.show();
                }
                else
                {
                    Toast toast = Toast.makeText
                            (Main2Activity.this, "Record failed to Insert " , Toast.LENGTH_LONG);
                    toast.show();
                }

The code for the Mileage insert follows exactly the same pattern. Here is the complete button listener from the Mileage activity.

 saveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //initialize all the text fields
                initilizeFields();
                //get the text values
                double miles=Double.parseDouble(mileage.getText().toString());
                double gals=Double.parseDouble(gallons.getText().toString());
                double dollars=Double.parseDouble(cost.getText().toString());
                String date=entryDate.getText().toString();
                int car=Integer.parseInt(carID.getText().toString());


                SqlHelper helper = new SqlHelper(MainActivity.this);
                SQLiteDatabase db= helper.getWritableDatabase();

                ContentValues values = new ContentValues();
                values.put(helper.MILEAGE_READING,miles);
                values.put(helper.TOTAL_GALLONS, gals);
                values.put(helper.TOTAL_COST, dollars);
                values.put(helper.MILEAGE_DATE,date);
                values.put(helper.VEHICLE_FK, car);

                //insert into mileage

                long mileage_Id=db.insert(helper.MILEAGE_TABLE, null, values);

                db.close();

                if(mileage_Id != -1)
                {
                    //start a toast (a message)
                    Toast toast = Toast.makeText
                            (MainActivity.this, "Record Added " + mileage_Id, Toast.LENGTH_LONG);
                    toast.show();
                }
                else
                {
                    Toast toast = Toast.makeText
                            (MainActivity.this, "Record failed to Insert " , Toast.LENGTH_LONG);
                    toast.show();
                }


                //calculate mileage if at least two values

            }
            private void initilizeFields(){
                mileage=(EditText)findViewById(R.id.editTextMileage);
                gallons=(EditText)findViewById(R.id.editTextGallons);
                cost=(EditText)findViewById(R.id.editTextCost);
                entryDate=(EditText)findViewById(R.id.editTextFillUpDate);
                carID=(EditText)findViewById(R.id.editTextCarID);
            }


        });

Next we will Look at how to query the data, do calculations and aggregates.

No comments:

Post a Comment