0

我正在我的 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...
}
4

1 回答 1

1

您的 TableLayout 是一个视图,只能在 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);
  }
  ...
}
于 2013-01-02T18:50:34.593 回答