1

I am not even sure if my code structure is appropriate. I have a trivia game that ends with the activity Results.java being displayed. I then have a Highscores.java with a subclass of DatabaseHelper that gets called to insert the current games score into the Highscores database table. Below is my Highscores.java class with the error commented in.

Also, if I should structure these classes differently - for example, move DatabaseHelper from a subclass to its own class - please let me know. I am having a difficult time implementing this SQLite database table.

Highscores.java

public class Highscores extends Activity {

    Context context;
    Button btn1;    
    DatabaseHelper dh;
    SQLiteDatabase db;

    private static final int DATABASE_VERSION = 1; 
    private static final String DB_NAME = "test3"; 
    private static final String DB_PATH = "/data/data/com.example.test/databases/";
    private static final String TABLE = "HighscoresList"; 

    // Table columns names. 
    private static final String RANK = "_id"; 
    private static final String SCORE = "score"; 
    private static final String PERCENTAGE = "percentage";

    TableLayout table;
    TableRow rowHeader, row1, row2, row3, row4, row5, row6, row7, row8, row9, row10;
    TextView rank, percentage, score;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscoresmain);

        Button btn1 = (Button)findViewById(R.id.homeBtn);

        btn1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(Highscores.this, MainMenu.class);
                startActivity(intent);
            }
        });

        DatabaseHelper(context);  //Error:  The method DatabaseHelper(Context) is undefined for the type Highscore.
    }

    public class DatabaseHelper extends SQLiteOpenHelper { 

        public DatabaseHelper(Context context) { 
            super(context, DB_NAME, null, DATABASE_VERSION); 
            db = getWritableDatabase();

            TableRow rowHeader = new TableRow(context);
            TableRow row1 = new TableRow(context);
            TableRow row2 = new TableRow(context);
            TableRow row3 = new TableRow(context);
            TableRow row4 = new TableRow(context);
            TableRow row5 = new TableRow(context);
            TableRow row6 = new TableRow(context);
            TableRow row7 = new TableRow(context);
            TableRow row8 = new TableRow(context);
            TableRow row9 = new TableRow(context);
            TableRow row10 = new TableRow(context);

            TextView rank = new TextView(context);
            TextView percentage = new TextView(context);
            TextView score = new TextView(context);
            TextView r1r = new TextView(context);
            TextView r1p = new TextView(context);
            TextView r1s = new TextView(context);

            Cursor c_percentage = db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE + ";", null);
            Cursor c_score = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + ";", null);

            rank.setText("TEST - COLUMN RANK");
            percentage.setText("TEST - COLUMN PERCENTAGE");
            score.setText("TEST - COLUMN SCORE");
            r1r.setText("test..rank");
            r1p.setText("teset...percentage");
            r1s.setText("test...scoree");

            rowHeader.addView(rank);
            rowHeader.addView(percentage);
            rowHeader.addView(score);

            row1.addView(r1r);
            row1.addView(r1p);
            row1.addView(r1s);

            table.addView(rowHeader);
            table.addView(row1);
            table.addView(row2);
            table.addView(row3);
            table.addView(row4);
            table.addView(row5);
            table.addView(row6);
            table.addView(row7);
            table.addView(row8);
            table.addView(row9);
            table.addView(row10);

            table = (TableLayout)findViewById(R.id.tableLayout);
        }

        //Check if new record makes the top 10.
        public boolean check(long score, int percentage) {
            Cursor c1 = db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE + " WHERE " + PERCENTAGE + "=" + percentage + ";", null);
            Cursor c2 = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + ";", null);
            if(c1 != null && c2 != null) {
                if(c1.getCount() > 0) {               
                    c2.moveToFirst();
                    int i = 0;
                    do {
                        i++;
                        long x = c2.getLong(c2.getColumnIndex("SCORE"));
                        if(x < percentage) {
                            if(c2.getCount() == 9) {
                                //Delete last record in high score and insert at index.
                                db.rawQuery("DELETE FROM " + TABLE + " WHERE " + RANK + " = 10;", null);
                                db.rawQuery("INSERT INTO " + TABLE + "VALUES (" + i + ", " + score + ", " + percentage + ");", null);
                                return true;
                            } else {
                                //No deletion - just insert.
                                db.rawQuery("INSERT INTO " + TABLE + "VALUES (" + i + ", " + score + ", " + percentage + ");", null);
                                return true;
                            }
                        } else {
                            return false;
                        }
                    } while (c2.moveToNext());
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        //Insert new record.
        public long insert(long score, int percentage) {
            ContentValues values = new ContentValues();
            values.put(SCORE, score);
            values.put(PERCENTAGE, percentage);

            return db.insert(TABLE, null, values);
        }

        public void openDatabase() throws SQLException {
            //Open the database.
            String myPath = DB_PATH + DB_NAME;
            db = SQLiteDatabase.openDatabase(myPath,  null,  SQLiteDatabase.OPEN_READONLY);
        }

        public synchronized void close() {
            if(db != null) {
                db.close();
            }
            super.close();
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE + " ("
                    + RANK + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + SCORE + " LONG,"
                    + PERCENTAGE + " INTEGER"
                    + ");");
        }
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
    }
4
  • 1
    If you are looking for general comments on the layout of your code, you should probably ask this at codereview.stackexchange.com. Commented Jan 2, 2013 at 17:30
  • 2
    If you want to access your database from any where in your app, you should move DatabaseHelper to it's own file. But the current problem is the context = null as ρяσѕρєя K stated. Also, you should always post your LogCat when your app crashes. Commented Jan 2, 2013 at 17:44
  • The file doesn't compile and run so there is no LogCat. So you are suggesting making Highscores and DatabaseHelper two separate classes? Commented Jan 2, 2013 at 17:53
  • Yes, but ρяσѕρєя K has already answered this, again. :) If you are replying to a particular user use the @Sam notation. This way we'll get a notification. (The author of the question or answer you commenting under gets a notification by default, which is why I didn't need to use @user1866707.) Commented Jan 2, 2013 at 18:48

1 Answer 1

4

use

 dh=new DatabaseHelper(Highscores.this);

instead of

 dh=new DatabaseHelper(context);

because you are not initialize context before passing it to DatabaseHelper constructor

OR

initialize context instance before passing it to DatabaseHelper constructor as in onCreate method of Highscores Activity :

context=Highscores.this;
Sign up to request clarification or add additional context in comments.

6 Comments

I tried both suggestions and a new error appears: ''''The method DatabaseHelper(Highscores) is undefined for the type Highscores.''''
@user1866707 : see my edit answer and plz add latest code also in your current post . as @ sam says in comment you have any issue if create a separate class for DatabaseHelper because you can easy use it in other components
or DatabaseHelper(getApplicationContext());
@SimonSchubert : current code have two issue first context is NULL and second is OP is calling Class constructor as Method
I will go ahead and follow what Sam said and implement DatabaseHelper as another class. I will try to integrate my program doing it that way and see what I come up with.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.