数据库适配器类;
//Primary Key for the score
public static final String KEY_ROWID = "id";
//Field for user score
public static final String KEY_SCORE = "score";
//Database name
private static final String DATABASE_NAME = "SportsQuizDB";
//Database table name
private static final String DATABASE_TABLE = "Score";
//Database version
private static final int DATABASE_VERSION = 1;
private dbHelper dbHelper;
private final Context context;
private SQLiteDatabase sqlDB;
private static class dbHelper extends SQLiteOpenHelper
{
//Constructor for dbHelper
public dbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_SCORE + " INTEGER NOT NULL);"
);
}
//This method is over ridden from its super class. If the
//database it out of date then it is updated in the constructor
//and logged here. Once updated the tables are dropped and
//onCreate is recalled.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Adapter(Context c)
{
context = c;
}
public Adapter open()
{
try{
dbHelper = new dbHelper(context);
sqlDB = dbHelper.getWritableDatabase();
}
catch (SQLException e)
{
Log.e("Open(); ",Log.getStackTraceString(e));
}
return this;
}
public Cursor selectTop3()
{
//I began to attempt the select form the SQLite DB
//But I am unsure how to use the cursor to populate the TextViews
String query = "SELECT * FROM "+ DATABASE_TABLE +" ORDER BY " + KEY_SCORE + " DESC ";
Cursor c = sqlDB.rawQuery(query, null);
return c;
}
public void close()
{
dbHelper.close();
}
包含3个TextView的HighScore类。
public class HighScores extends Activity {
TextView result1, result2, result3;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Removes Title Bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Removes Notification Bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_high_scores);
result1 = (TextView) findViewById(R.id.result1);
result2 = (TextView) findViewById(R.id.result2);
result3 = (TextView) findViewById(R.id.result3);
fillHighScores();
}
private void fillHighScores() {
openDB();
//This it where I wish to populate the three TextViews (result1, result2, result3)
closeDB();
}
private void openDB() {
adapter = new Adapter(this);
adapter.open();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void closeDB() {
adapter.close();
}
@Override
protected void onPause() {
super.onPause();
}
我现在已经尝试了2天似乎是一个简单的过程。在我看来,android SQLite过于混乱和冗长。如果有人能发光,那将是非常感激的。提前谢谢。
答案 0 :(得分:0)
我不确定您要使用适配器完成什么,但您需要的是SQLiteOpenHelper的实例并调用函数getReadableDatabase。使用数据库,您可以查询前3个结果。
这是一个很棒的教程http://www.vogella.com/tutorials/AndroidSQLite/article.html
解析光标可能很痛苦。您可能想要查看几个库。 Green DAO和ORM Lite。