私は、ローカルデータベースにハイスコアを保持するゲームを作ろうとしており、最後のスコアがdbのスコアよりも大きい場合にはそれを更新します。しかし、私が理解できなかった小さな問題があります。 dbから値を取得できず、cursor.getCount()は常に0を返します。私は、dbが作成されたばかりの、最初のハイスコアの初期値を0にします。事前にAndroidのSQLiteでCRUDを作成できない
package com.example.recepinanc.countdowntimer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by recepinanc on 29/03/16.
*/
public class DBHelper extends SQLiteOpenHelper {
private static String DATABASE_NAME = "database_name";
private static String TABLE_NAME = "table_name";
private static String PLAYER_NAME = "player_name";
private static String PLAYER_HIGHSCORE = "player_high_score";
private static String PLAYER_ID = "player_id";
private static String CREATE_PLAYER_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + PLAYER_ID + " INTEGER PRIMARY KEY, " + PLAYER_NAME + " TEXT, " + PLAYER_HIGHSCORE + " INTEGER DEFAULT 0)";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_PLAYER_TABLE);
}
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + CREATE_PLAYER_TABLE);
onCreate(db);
}
public void insertHighScore(int score) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PLAYER_HIGHSCORE, score);
db.insert(TABLE_NAME, null, values);
db.close();
}
public int getHighScore() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[]{PLAYER_HIGHSCORE}, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
Log.i("Get Count : ", cursor.getCount() + "");
}
return cursor.getInt(2);
}
public void updateHighScore(int newHighScore) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("UPDATE " + TABLE_NAME + " SET " + PLAYER_HIGHSCORE + "=" + newHighScore + " WHERE " + PLAYER_ID + "=1");
Log.i("UPDATE","UPDATED !");
db.close();
}
}
public class GameOver extends Activity {
DBHelper dbhelper;
static int highScore;
TextView best;
ImageView repeat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_over_layout);
dbhelper = new DBHelper(getApplicationContext());
highScore = dbhelper.getHighScore();
repeat = (ImageView) findViewById(R.id.play_again);
best = (TextView) findViewById(R.id.best_score_tv);
best.setText(dbhelper.getHighScore()+"");
int score = getIntent().getIntExtra(MainActivity.CLICK_COUNTS, 0);
dbhelper.insertHighScore(score);
if (score > highScore) {
dbhelper.updateHighScore(score);
Log.i("HighScoreNew", dbhelper.getHighScore() + "");
}
repeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GameOver.this, MainActivity.class);
startActivity(intent);
finish();
}
});
TextView clickCountTextView = (TextView) findViewById(R.id.clickCounts_TextView);
clickCountTextView.setText(score + "");
}
}
ありがとう:
は、ここに私のコードです!
なぜダウンボートを取得しましたか? – Recomer