我正在我的 DatabaseHelper 类中创建一个数据库。我在这个类中创建了 textview、tablerows 和 tablelayout。我还将 textviews 添加到 tablerows 并将 tablerows 添加到 tablelayout。
我的问题是如何从另一个类中调用此表格布局并将其显示在活动中。我进行了一些研究,似乎他们希望我编写我在下面注释掉的代码行。从被注释掉的那行来看,我似乎无法实现它,因为这个类没有活动/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...
}