To Lecture Notes

IT 372 -- May 20, 2019

Review Exercises

  1. Write a Java statement that clears out all the rows of a database table. Ans:
    db.execSQL("delete from students");
    
  2. If column 1 of a database table is id, write a Java statement that will create a cursor that contains the data in the row specified by the id in an EditText widget. Ans:
    Cursor c = db.rawQuery("select * from students where id = ?", new String[ ] {"id"} );
    

Practice Problems

  1. Finish Problem 5 of the May 15 notes. Ans: Here are the project files
    activity_main.xml   MainActivity.java   MyView.java
    PointsDBHelper.java   DBRow.java
  2. Modify Problem 1 to show the row ID in a toast.
    Ans: Replace
    System.out.println("Found ID: " + foundID);
    
    by
    (Toast.makeText(savedContext, "Found ID:" + foundID, 
        Toast.LENGTH_SHORT)).show( );
    
  3. Modify Problem 1 to show the row ID in a TextView widget on a new activity.
    Ans: Replace the toast code in the previous problem by:
    Intent intent = new Intent(savedContext, OutputActivity.class);
    intent.putExtra("key", foundID);
    savedContext.startActivity(intent);
    
    In the Output activity file OutputActivity.java, add this code to the end of the onCreate method:
    Intent intent = getIntent( );
    int id = intent.getIntExtra("key", 1);
    TextView tv = (TextView) findViewById(R.id.txt_output);
    tv.setText(String.valueOf(id));
    
  4. Modify Problem 1 to show the contents of the row with the row ID on a TextView widget on a new activity.
    Ans: In the OutputActivity.java file, replace the line
    tv.setText(String.valueOf(id));
    
    by
    PointsDBHelper pdbh = new PointsDBHelper(this);
    SQLiteDatabase db = pdbh.getReadableDatabase( );
    Cursor c = db.rawQuery("select * from points where id = ?",
        new String[ ] {String.valueOf(id)} );
    String output = "Unintialized output";
    if (c.moveToFirst( )) {
        int index = c.getInt(0);
        float x = c.getFloat(1);
        float y = c.getFloat(2);
        output = String.format(Locale.getDefault( ),
            "%d  %.2f  %.2f", index, x, y);
    }
    else {
        output = "Row not found.";
    }
    tv.setText(output);
    

Setting the App Icon

Themes