1

我什至不确定我的代码结构是否合适。我有一个以显示活动 Results.java 结尾的琐事游戏。然后我有一个带有 DatabaseHelper 子类的 Highscores.java,它被调用以将当前游戏分数插入到 Highscores 数据库表中。下面是我的 Highscores.java 类,其中注释了错误。

另外,如果我应该以不同的方式构建这些类——例如,将 DatabaseHelper 从一个子类移动到它自己的类——请告诉我。我很难实现这个 SQLite 数据库表。

高分.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 回答 1

4

利用

 dh=new DatabaseHelper(Highscores.this);

代替

 dh=new DatabaseHelper(context);

context因为在将其传递给DatabaseHelper构造函数之前您没有初始化

或者

在将上下文实例传递给 DatabaseHelper构造函数之前初始化上下文实例,如Activity的onCreate方法:Highscores

context=Highscores.this;
于 2013-01-02T17:32:05.843 回答