2To Examples

StudentsDb2 Example

Goal:

Directions:

  1. Create an Android Studio project with name StudentsDb2 and package it372.studentsdb2.
  2. Set up a layout that looks similar to this when displayed in an emulator.
  3. Set the ids of the EditText and TextView widgets like this:

    Type ID Text or Hint
    EditText edtxtName Enter Name:
    EditText edtxtGrade Enter Grade:
    EditText edtxtGender Enter Gender:

  4. Set the text and the onClick attributes of the buttons like this:
    Type Text onClick
    Button Enter Data in DB onClickEnterData
    Button           << onClickFirst
    Button           < onClickPrev
    Button           > onClickNext
    Button           >> onClickLast
  5. Create this StudentsDBHelper class derived from the SQLiteOpenHelper class:
         StudentsDbHelper.java
  6. Add this Java code to the MainActivity.java class:
    1. Define these private instance variables at the top of the class:
      private SQLiteDatabase db;
      private EditText edtxtName, edtxtGender, edtxtGrade;
      private TextView txtName, txtGrade, txtGender;
      private Cursor cursor;
      
    2. Add this code to the OnCreate method:
      SQLiteOpenHelper dbh = new StudentsDbHelper(this);
      try {
          db = dbh.getWritableDatabase();
      }
      catch(SQLiteException e) {
          Toast toast = Toast.makeText(this,
              "Database not created.", Toast.LENGTH_LONG);
      }
              
      // System.out.println("In OnCreate");
      edtxtName = findViewById(R.id.edtxt_name);
      edtxtGrade = findViewById(R.id.edtxt_grade);
      edtxtGender = findViewById(R.id.edtxt_gender);
      txtName = findViewById(R.id.txt_name);
      txtGrade = findViewById(R.id.txt_grade);
      txtGender = findViewById(R.id.txt_gender);
      
      cursor = db.query("students",
          new String[ ] {"name", "grade", "gender"},
          null, null, null, null, null);
      
    3. Create event handlers stubs for the buttons with these names:
      onClickEnterData  onClickFirst  onClickPrev
      
      onClickNext  onClickLast
      
      You can test the stubs like this:
      // Event handlers with accessibility public
      // work better than protected.
      public void onClickEnterData(View view) {
          System.out.println("In onClickEnterData");
      }
      
    4. Add lines to onClickEnterData that will

      get the data from the EditText widgets:
      String name = edtxtName.getText( ).toString( );
      int grade = Integer.parseInt(edtxtGrade.getText( ).toString( ));
      String gender = edtxtGender.getText( ).toString( );
      
      set up a ContentValues object:
      ContentValues studentValues = new ContentValues( );
      
      insert the data into the ContentValues object:
      studentValues.put("name", name);
      studentValues.put("grade", grade);
      studentValues.put("gender", gender);
      
      insert the data into the SQL students table:
      db.insert("students", null, studentValues);
      
      clear the data from the EditText widgets:
      edtxtName.setText("");
      edtxtGrade.setText("");
      edtxtGender.setText("");
      
      set up a cursor to navigate the students table:
      cursor = db.query("students",
          new String[ ] {"name", "grade", "gender"},
          null, null, null, null, null);
      
      The null arguments correspond to these parameters:
      selection  selectionArgs  groupBy  
      having  orderBy
      
  7. Run the app to test it.
  8. Locate the database using the Device File Explorer:
    View >> Tool Windows >> Device File Explorer
    Find the students.db database:
    /data/data/it372/ssmiths/studentsdb2/databases/students.db
  9. If necessary, delete the database using the Device File Explorer to start over with an empty database.
  10. Set up the other event handlers onClickFirst, onClickPrev, onClickLast, onClickLast.
    Use these Cursor methods:
    moveToFirst  moveToPrevious  moveToNext  moveToLast
    
    isFirst  isLast  getString  getInt
    
    Here is the moveToFirst event handler:
    public void onClickFirst(View view) {
        if (cursor.moveToFirst()) {
            String name = cursor.getString(0);
            int grade = cursor.getInt(1);
            String gender = cursor.getString(2);
            txtName.setText(name);
            txtGrade.setText(String.valueOf(grade));
            txtGender.setText(gender);
        }
        else {
            txtName.setText("");
            txtGrade.setText("");
            txtGender.setText("");
        }
    }
    
  11. Here are the final layout, strings resource, and Java activity files:
        activity_main.xml  strings.xml  MainActivity.java