我想创建一个课程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;
}
}
类HighScore:
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时,它会引发错误。
任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
您需要发布错误,但我看到的第一件事是您没有将主键列名设置为“_id”。
这是Android在几乎任何小部件中使用此数据所必需的。它是如何跟踪数据的。您不仅需要在DB中使用它,还必须调用它,以便它可以用于备份适配器的任何游标中。你不需要显示它,但它必须在那里。