我想创建一个类,HighScore
来保存游戏的分数。我使用SQLite
和ListView
显示。这是我的代码:
类 HighScoreData:
public class HighscoreData {
public static final String SID = "scoreId";
public static final String SLEVEL = "level";
public static final String SSCORE = "score";
public static final String DB_NAME = "ScoreDB";
public static final String DB_TABLE = "Score";
public static final int DB_VERSION = 2;
private final Context mContext;
private SQLiteDatabase mDB;
private DBHelper mDBHelper;
public HighscoreData(Context c)
{
this.mContext = c;
}
private static class DBHelper extends SQLiteOpenHelper
{
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
try
{
db.execSQL("CREATE TABLE Score(scoreId integer PRIMARY KEY autoincrement, level int, score int);");
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("DBAdapter", "Updating database...");
db.execSQL("DROP TABLE IF EXISTS Student");
onCreate(db);
}
}
/**
*
*/
public HighscoreData openDB()
{
mDBHelper = new DBHelper(mContext, DB_NAME, null, DB_VERSION);
mDB = mDBHelper.getWritableDatabase();
return this;
}
/**
* close
*/
public void closeDB()
{
mDBHelper.close();
}
/**
* insert record
*/
public long insert(int level, int score)
{
ContentValues cv = new ContentValues();
cv.put(SLEVEL, level);
cv.put(SSCORE, score);
return mDB.insert(DB_TABLE, null, cv);
}
/**
*
*/
public boolean edit(int _id, int level, int score)
{
ContentValues cv = new ContentValues();
cv.put(SLEVEL, level);
cv.put(SSCORE, score);
return mDB.update(DB_TABLE, cv, SID + "=" + _id, null) > 0;
}
/**
*
*/
public boolean remove(int _id)
{
return mDB.delete(DB_TABLE, SID + "=" + _id, null) > 0;
}
/**
*
*/
public void removeAll()
{
mDB.delete(DB_TABLE, null, null);
}
/**
*
*/
public Cursor getAllScores()
{
return mDB.query(DB_TABLE, new String[]{SID, SLEVEL, SSCORE}, null, null, null, null, null);
}
/**
*
*/
public Cursor getScoreById(int _id)
{
Cursor mCursor = mDB.query(true, DB_TABLE, new String[]{SID, SLEVEL, SSCORE}, SID + "=" + _id, null, null, null, null, null);
if(mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
}
类高分:
public class Highscore extends Activity {
private Cursor mCursor;
private ListView listView;
String level;
String score;
String str = new String();
StringBuilder sb = new StringBuilder();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
mCursor = BeginGame.hsData.getAllScores();
final ArrayList<String> arrayWork = new ArrayList<String>();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item, arrayWork);
listView = (ListView)findViewById(R.id.hScore);
if(mCursor.moveToFirst()) {
do{
level =String.valueOf(mCursor.getInt(1));
score =String.valueOf( mCursor.getInt(2));
sb.append("Level :").append(level).append(" - ");
sb.append("Score :").append(score).append("\n");
}while(mCursor.moveToNext());
}
str = sb.toString();
arrayWork.add(str);
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
当我在 SQLite 中保存分数时,我可以打开HighScore
活动,但是当我第一次打开 SDK 时,它会抛出错误。
任何帮助,将不胜感激。谢谢。