Skip to content
Browse files

Added FootyLinksDBAdapter class (needs refactoring).

Wired up DB to app so that app can access data!
Note, passwords are not supported on the database.
  • Loading branch information...
1 parent d31b7e4 commit 13522276c77c8fdd6a696b1cca04256a081d0d3a @MikeHook committed Jul 3, 2011
View
5 AndroidApp/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,5 @@
+#Sat Jul 02 14:36:37 BST 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.source=1.5
View
BIN AndroidApp/assets/footylinks.db
Binary file not shown.
View
3 AndroidApp/res/layout/one_degree.xml
@@ -18,8 +18,7 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dip"
- android:layout_below="@id/text_game_title"
- android:text="Joe Cole"
+ android:layout_below="@id/text_game_title"
/>
<TextView
android:id="@+id/text_previously_played"
View
224 AndroidApp/src/mhook/FootyLinks/FootyLinksDbAdapter.java
@@ -0,0 +1,224 @@
+package mhook.FootyLinks;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.SQLException;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteException;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.util.Log;
+
+/**
+ * Database access helper class.
+ * Work in progress
+ */
+public class FootyLinksDbAdapter {
+
+ public static class PlayerColumns {
+ public static final String Name = "Name";
+ public static final String CurrentClub_id = "CurrentClub_id";
+ public static final String _id = "_id";
+ }
+
+ private static final String TAG = "FootyLinksDbAdapter";
+ private SQLiteDatabase sqlLiteDatabase;
+
+ private static final String DATABASE_FOLDER = "/data/data/mhook.FootyLinks/databases/";
+ private static final String DATABASE_NAME = "footylinks.db";
+ private static final String PLAYER_TABLE = "Player";
+ private static final int DATABASE_VERSION = 3;
+
+ private final Context context;
+
+ private static class FootyLinksHelper extends SQLiteOpenHelper {
+
+ private Context myContext;
+
+ FootyLinksHelper(Context context) {
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
+
+ this.myContext = context;
+ }
+
+ /**
+ * Creates a empty database on the system and rewrites it with your own database.
+ * */
+ public void createDataBase() throws IOException{
+
+ boolean dbExist = checkDataBase();
+
+ if(dbExist){
+ //do nothing - database already exist
+ }else{
+ //By calling this method and empty database will be created into the default system path
+ //of your application so we are gonna be able to overwrite that database with our database.
+ this.getReadableDatabase();
+
+ try {
+ copyDataBase();
+ } catch (IOException e) {
+ throw new Error("Error copying database");
+ }
+ }
+ }
+
+ /**
+ * Check if the database already exist to avoid re-copying the file each time you open the application.
+ * @return true if it exists, false if it doesn't
+ */
+ private boolean checkDataBase(){
+
+ SQLiteDatabase sqlLiteDatabase = null;
+ try
+ {
+ sqlLiteDatabase = openDatabase();
+ }
+ catch(SQLiteException e){
+ //database does't exist yet.
+ }
+
+ if(sqlLiteDatabase != null)
+ {
+ sqlLiteDatabase.close();
+ }
+
+ return sqlLiteDatabase != null ? true : false;
+ }
+
+ /**
+ * Copies your database from your local assets-folder to the just created empty database in the
+ * system folder, from where it can be accessed and handled.
+ * This is done by transfering bytestream.
+ * */
+ private void copyDataBase() throws IOException{
+
+ //Open your local db as the input stream
+ InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
+
+ // Path to the just created empty db
+ String outFileName = DATABASE_FOLDER + DATABASE_NAME;
+
+ //Open the empty db as the output stream
+ OutputStream myOutput = new FileOutputStream(outFileName);
+
+ //transfer bytes from the inputfile to the outputfile
+ byte[] buffer = new byte[1024];
+ int length;
+ while ((length = myInput.read(buffer))>0){
+ myOutput.write(buffer, 0, length);
+ }
+
+ //Close the streams
+ myOutput.flush();
+ myOutput.close();
+ myInput.close();
+ }
+
+ public SQLiteDatabase openDatabase() throws SQLException{
+
+ //Open the database
+ String myPath = DATABASE_FOLDER + DATABASE_NAME;
+ return SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+
+ }
+
+ // Add your public helper methods to access and get content from the database.
+ // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
+ // to you to create adapters for your views.
+
+ }
+
+
+ /**
+ * Constructor - takes the context to allow the database to be
+ * opened/created
+ *
+ * @param ctx the Context within which to work
+ */
+ public FootyLinksDbAdapter(Context ctx) {
+ this.context = ctx;
+ }
+
+ /**
+ * Open the notes database. If it cannot be opened, try to create a new
+ * instance of the database. If it cannot be created, throw an exception to
+ * signal the failure
+ *
+ * @return this (self reference, allowing this to be chained in an
+ * Initialisation call)
+ * @throws SQLException if the database could be neither opened or created
+ */
+ public FootyLinksDbAdapter open() throws SQLException {
+ FootyLinksHelper myDbHelper = new FootyLinksHelper(this.context);
+ try {
+ myDbHelper.createDataBase();
+
+ } catch (IOException ioe) {
+ throw new Error("Unable to create database");
+ }
+
+ sqlLiteDatabase = myDbHelper.openDatabase();
+
+ return this;
+ }
+
+ public void close() {
+ sqlLiteDatabase.close();
+ }
+
+ /**
+ * Return a Cursor positioned at the note that matches the given rowId
+ *
+ * @param Id of player to retrieve
+ * @return Cursor positioned to matching note, if found
+ * @throws SQLException if note could not be found/retrieved
+ */
+ public Cursor fetchPlayer(long Id) throws SQLException {
+
+ Cursor mCursor =
+ sqlLiteDatabase.query(true, PLAYER_TABLE, new String[] {PlayerColumns._id,
+ PlayerColumns.Name, PlayerColumns.CurrentClub_id}, PlayerColumns._id + "=" + Id,
+ null, null, null, null, null);
+ if (mCursor != null) {
+ mCursor.moveToFirst();
+ }
+ return mCursor;
+
+ }
+
+ /**
+ * Return a Cursor positioned at the note that matches the given rowId *
+ * @param rowId id of note to retrieve
+ * @return Cursor positioned to matching note, if found
+ * @throws SQLException if note could not be found/retrieved
+ */
+ /*
+ public Cursor fetchNote(long rowId) throws SQLException {
+
+ Cursor mCursor =
+
+ mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
+ KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
+ null, null, null, null);
+ if (mCursor != null) {
+ mCursor.moveToFirst();
+ }
+ return mCursor;
+
+ }*/
+}
View
26 AndroidApp/src/mhook/FootyLinks/OneDegree.java
@@ -2,30 +2,48 @@
import android.app.Activity;
import android.content.Intent;
+import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
-import android.widget.ArrayAdapter;
import android.widget.Button;
-import android.widget.Spinner;
+import android.widget.TextView;
import android.widget.Toast;
public class OneDegree extends Activity {
+ private FootyLinksDbAdapter footyLinksDbAdapter;
+ private TextView startPlayerTextView;
+
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.one_degree);
+ setContentView(R.layout.one_degree);
+
+ footyLinksDbAdapter = new FootyLinksDbAdapter(this);
+
+ footyLinksDbAdapter.open();
+ startPlayerTextView = (TextView) findViewById(R.id.text_start_player);
+
+ populateFields();
Button guess1Button = (Button) findViewById(R.id.button_guess1);
guess1Button.setOnClickListener(new Guess1ButtonClickListener());
}
+ private void populateFields() {
+
+ Cursor footyLinksDbCursor = footyLinksDbAdapter.fetchPlayer(49842);
+ String startPlayerName = footyLinksDbCursor.getString(
+ footyLinksDbCursor.getColumnIndexOrThrow(FootyLinksDbAdapter.PlayerColumns.Name));
+
+ startPlayerTextView.setText(startPlayerName);
+ }
+
public class Guess1ButtonClickListener implements OnClickListener {
- @Override
public void onClick(View v) {
Intent pickClubIntent = new Intent(OneDegree.this, PickClub.class);
View
1 AndroidApp/src/mhook/FootyLinks/StartGame.java
@@ -31,7 +31,6 @@ public void onCreate(Bundle savedInstanceState) {
public class StartButtonClickListener implements OnClickListener {
- @Override
public void onClick(View v) {
Intent intent = new Intent(StartGame.this, OneDegree.class);
View
BIN Database/footylinks.db
Binary file not shown.

0 comments on commit 1352227

Please sign in to comment.
Something went wrong with that request. Please try again.