- 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
- 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( );
- 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));
- 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);