我在DatabaseHelper类中创建了一个数据库。我在这个类中创建了textview,tablerows和tablelayout。我还将textviews添加到tablerows和tablerowout的表格中。
我的问题是如何从另一个类调用此tablelayout并将其显示在活动中。我研究了一下,似乎他们希望我写下我在下面评论过的代码行。从注释掉的行中,似乎我没有实现它,因为这个类没有activity / xml文件。
希望我解释得那么好。
public class DatabaseHelper extends SQLiteOpenHelper {
Context context;
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;
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)contextActivity.findViewById(R.id.tableLayout);
}
//more methods and code below...
}
答案 0 :(得分:1)
您的TableLayout是一个View,只能在Activity中使用(用于显示)。因此,将与您的TableLayout相关的所有代码放在要显示此表的活动中。使用DatabaseHelper类(顾名思义)仅用于访问数据库的帮助。就这样:
public class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper dh;
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";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
}
public SQLiteDatabase openDb() {
return this.getReadableDatabase(); // Use Readable because you're not actually writing any values into your db
}
public void closeDb(SQLiteDatabase db) {
db.close();
}
public Cursor getScore(SQLiteDatabase db) {
return db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + ";", null);
}
public Cursor getPercentage(SQLiteDatabase db) {
return db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE + ";", null);
}
}
现在,从您的活动中,您可以执行以下操作:
public class Blah extends Activity {
...
public void drawTable() {
TableLayout table = (TableLayout)findViewById(R.id.tableLayout);
... // initialize all views as before
DatabaseHelper helper = new DatabaseHelper(this);
SQLiteDatabase db = helper.openDb();
Cursor scores = helper.getScore(db);
Cursor percentages = helper.getPercentage(db);
... // do something with the Cursors
..
// finally
scores.close();
percentages.close();
helper.closeDb(db);
}
...
}