Solution to W5L1 homework

To complete the Database homework due W5L1, I made the following modifications
to the EventsV2 codebase. This took about 30 minutes with a lot of cutting
and pasting and minor editing (and with a glass of wine!) Everything seems pretty
straightforward...

First, I replaced the old database in EventV2 (which contained an id, the time, and a title string) with a new database that could be created as CREATE TABLE events( id INTEGER PRIMARY KEY AUTOINCREMENT, uname TEXT NOT NULL, uscore INTEGER, fscore INTEGER, time LONG, gtime INTEGER); Next, when the onCreate method of the main activity starts
the application would insert random data into the table ... This required making the following modifications in four files. The actual diffs appear after this synopsis of the changes: **************************************************************** [dyn-129-64-87-200:pragmatic/code/Eventsv2] tim% git diff --stat res/layout/item.xml | 54 +++++++++++++++++++++++++++---- src/org/example/events/Constants.java | 5 ++- src/org/example/events/Events.java | 21 ++++++++---- src/org/example/events/EventsData.java | 14 ++++++-- 4 files changed, 76 insertions(+), 18 deletions(-) **************************************************************** /res/layout/item.xml had to be modified to include TextView elements for each of the new columns in the database /src/org/example/events/Constants.java had to be modified to add constants for the names of the new columns (and I deleted the constants for the old columns) /src/org/example/events/Events.java This had several changes: * import the necessary constants from Constants.java * update the FROM string to include the new column names * update the TO string to include the new view names (created in item.xml) * replace the addEvent("hello android") with an addEvent that takes four arguments and creates a row in the new table (using Math.random()) * modify the addEvent method to add additional parameters (one for each new column of the table) and change the code to add the new values into the ContentValues object /src/org/example/events/EventsData.java * import the new Constants * change the onCreate method to create the new table with six columns rather than the old one with three **************************************************************** The precise changes are listed below in the diff file ... % CHANGES in /res/layout/item.xml diff --git a/res/layout/item.xml b/res/layout/item.xml index 40a69d2..fb1fe60 100644 --- a/res/layout/item.xml +++ b/res/layout/item.xml @@ -24,22 +24,62 @@ android:text=": " android:layout_toRightOf="@id/rowid" /> <TextView - android:id="@+id/time" + android:id="@+id/uname" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:ellipsize="end" + android:singleLine="true" + android:textStyle="italic" android:layout_toRightOf="@id/rowidcolon" /> <TextView + android:id="@+id/unamecolon" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text=": " + android:layout_toRightOf="@id/uname" /> + <TextView + android:id="@+id/uscore" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_toRightOf="@id/unamecolon" /> + <TextView + android:id="@+id/uscorecolon" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text=": " + android:layout_toRightOf="@id/uscore" /> + <TextView + android:id="@+id/fscore" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_toRightOf="@id/uscorecolon" /> + <TextView + android:id="@+id/fscorecolon" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text=": " + android:layout_toRightOf="@id/fscore" /> + <TextView + android:id="@+id/time" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_toRightOf="@id/fscorecolon" /> + <TextView android:id="@+id/timecolon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=": " android:layout_toRightOf="@id/time" /> - <TextView - android:id="@+id/title" - android:layout_width="fill_parent" + <TextView + android:id="@+id/gtime" + android:layout_width="wrap_content" android:layout_height="wrap_content" - android:ellipsize="end" - android:singleLine="true" - android:textStyle="italic" android:layout_toRightOf="@id/timecolon" /> + <TextView + android:id="@+id/gtimecolon" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text=": " + android:layout_toRightOf="@id/gtime" /> + </RelativeLayout> \ No newline at end of file diff --git a/src/org/example/events/Constants.java b/src/org/example/events/Constants.java index e64fa4a..3293e5f 100644 --- a/src/org/example/events/Constants.java +++ b/src/org/example/events/Constants.java @@ -15,5 +15,8 @@ public interface Constants extends BaseColumns { // Columns in the Events database public static final String TIME = "time"; - public static final String TITLE = "title"; + public static final String UNAME = "uname"; + public static final String USCORE = "uscore"; + public static final String FSCORE = "fscore"; + public static final String GTIME = "gtime"; } diff --git a/src/org/example/events/Events.java b/src/org/example/events/Events.java index bec7634..2b20d51 100644 --- a/src/org/example/events/Events.java +++ b/src/org/example/events/Events.java @@ -11,7 +11,10 @@ package org.example.events; import static android.provider.BaseColumns._ID; import static org.example.events.Constants.TABLE_NAME; import static org.example.events.Constants.TIME; -import static org.example.events.Constants.TITLE; +import static org.example.events.Constants.UNAME; +import static org.example.events.Constants.USCORE; +import static org.example.events.Constants.FSCORE; +import static org.example.events.Constants.GTIME; import android.app.ListActivity; // ... @@ -29,10 +32,10 @@ import android.widget.SimpleCursorAdapter; public class Events extends ListActivity { // ... - private static String[] FROM = { _ID, TIME, TITLE, }; + private static String[] FROM = { _ID, UNAME, USCORE, FSCORE, TIME, GTIME, }; private static String ORDER_BY = TIME + " DESC"; - private static int[] TO = { R.id.rowid, R.id.time, R.id.title, }; + private static int[] TO = { R.id.rowid, R.id.uname, R.id.uscore, R.id.fscore, R.id.time, R.id.gtime, }; private EventsData events; @@ -42,7 +45,7 @@ public class Events extends ListActivity { setContentView(R.layout.main); events = new EventsData(this); try { - addEvent("Hello, Android!"); + addEvent("tim",(int)Math.round(20*Math.random()),(int)Math.round(20*Math.random()),10+(int)Math.round(5*Math.random())); Cursor cursor = getEvents(); showEvents(cursor); } finally { @@ -50,13 +53,17 @@ public class Events extends ListActivity { } } - private void addEvent(String string) { + private void addEvent(String uname, int uscore, int fscore, int gtime) { // Insert a new record into the Events data source. // You would do something similar for delete and update. SQLiteDatabase db = events.getWritableDatabase(); ContentValues values = new ContentValues(); - values.put(TIME, System.currentTimeMillis()); - values.put(TITLE, string); + values.put(UNAME,uname); + values.put(USCORE,uscore); + values.put(FSCORE,fscore); + values.put(TIME,System.currentTimeMillis()); + values.put(GTIME,gtime); + db.insertOrThrow(TABLE_NAME, null, values); } diff --git a/src/org/example/events/EventsData.java b/src/org/example/events/EventsData.java index 58021bc..0dafb39 100644 --- a/src/org/example/events/EventsData.java +++ b/src/org/example/events/EventsData.java @@ -11,7 +11,10 @@ package org.example.events; import static android.provider.BaseColumns._ID; import static org.example.events.Constants.TABLE_NAME; import static org.example.events.Constants.TIME; -import static org.example.events.Constants.TITLE; +import static org.example.events.Constants.UNAME; +import static org.example.events.Constants.USCORE; +import static org.example.events.Constants.FSCORE; +import static org.example.events.Constants.GTIME; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; @@ -28,8 +31,13 @@ public class EventsData extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID - + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME - + " INTEGER," + TITLE + " TEXT NOT NULL);"); + + " INTEGER PRIMARY KEY AUTOINCREMENT, " + + UNAME + " TEXT NOT NULL," + + USCORE + " INTEGER," + + FSCORE + " INTEGER," + + TIME + " LONG," + + GTIME + " INTEGER);" ); + } @Override
Comments