从超类调用子类构造函数 - Android(java)

时间:2013-01-02 17:29:05

标签: java android subclass superclass

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

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

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) {}
    }

1 个答案:

答案 0 :(得分:4)

使用

 dh=new DatabaseHelper(Highscores.this);

而不是

 dh=new DatabaseHelper(context);

因为在将context传递给DatabaseHelper构造函数

之前没有初始化DatabaseHelper

初始化上下文实例,然后将其传递给onCreate构造函数,如Highscores活动的context=Highscores.this; 方法:

{{1}}