這是我無法弄清楚的。我曾計劃使用SQLite數據庫在玩家達到0次生命後保存並檢索ListView中的每個分數,因爲它是無限級別。但是,所有的分數都返回給我0.我不知道這個問題。SQLite數據庫在ListView中保存和檢索得分,但得到0分
這裏是我的DBHelper,
public class DBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "highscore.db";
private static final String TABLE_HIGHSCORE = "highscore";
private static final String COLUMN_ID = "id";
private static final String COLUMN_SCORE = "score";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableSql = "CREATE TABLE " + TABLE_HIGHSCORE + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_SCORE + " INTEGER " + ");";
db.execSQL(createTableSql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_HIGHSCORE);
onCreate(db);
}
public void addHighScore(int highscore) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_SCORE, highscore);
db.insert(TABLE_HIGHSCORE,null,values);
db.close();
}
public ArrayList<Scores> getAllScores() {
ArrayList<Scores> scoresList = new ArrayList<>();
String selectQuery = "SELECT " + COLUMN_ID + ", " + COLUMN_SCORE + " FROM " + TABLE_HIGHSCORE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(0);
int score = cursor.getInt(1);
Scores scores = new Scores(id,score);
scoresList.add(scores);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return scoresList;
}
}
你在哪裏初始化代碼中的生活?我猜這就是問題所在,因爲它總是返回相同的分數,即0 – Mandy8055
我把它放在全局像左= 10;即使得分也是相同的,但我只初始化爲0 –
我應該怎麼知道你是零,發完整的代碼 – Salman500