我正在尝试在游戏中编写代码以在计时器超时后保存分数。我有我的分数,我有我的计时器。我已经阅读了一些关于SharedPreferences和SQLite的内容。我知道我想保存前10名的高分,但SharedPreferences最好只获得1分。我试图围绕SQLite,但我无法得到它。任何人都可以帮助我找出一行代码来保存前10个分数。
继承我的onFinish代码:
public void onFinish() {
textViewTimer.setText("00:000");
timerProcessing[0] = false;
/** This is the Interstitial Ad after the game */
AppLovinInterstitialAd.show(GameActivity.this);
/** This creates the alert dialog when timer runs out */
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(GameActivity.this);
alertDialogBuilder.setTitle("Game Over");
alertDialogBuilder.setMessage("Score: " + count);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setNeutralButton("Restart", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent restartGame = new Intent(GameActivity.this, GameActivity.class);
startActivity(restartGame);
finish();
}
});
alertDialogBuilder.setNegativeButton("Home", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();
}
});
alertDialogBuilder.show();
答案 0 :(得分:4)
您可以使用下面给出的代码保存任意数量的分数并检索它们。创建一个名为DatabaseHandler.java的新数据库帮助程序类,并在其中添加以下代码。
要初始化您的活动中的课程,请在您的活动中添加以下内容:
DatabaseHandler db = new DatabaseHandler(this);
然后为db use添加值db.addScore(count);
要从db中过滤掉前十个分数,您可以将get方法中的查询更改为:
String selectQuery = "SELECT * FROM " + TABLE_SCORE + "LIMIT 10";
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "game";
// Table name
private static final String TABLE_SCORE = "score";
// Score Table Columns names
private static final String KEY_ID_SCORE = "_id";
private static final String KEY_SCORE = "score_value";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_SCORE_TABLE = "CREATE TABLE " + TABLE_SCORE + "("
+ KEY_ID_SCORE + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_SCORE + " TEXT" + ")";
db.execSQL(CREATE_SCORE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE);
// Create tables again
onCreate(db);
}
// Adding new score
public void addScore(int score) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SCORE, score); // score value
// Inserting Values
db.insert(TABLE_SCORE, null, values);
db.close();
}
// Getting All Scores
public String[] getAllScores() {
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_SCORE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
int i = 0;
String[] data = new String[cursor.getCount()];
while (cursor.moveToNext()) {
data[i] = cursor.getString(1);
i = i++;
}
cursor.close();
db.close();
// return score array
return data;
}
}
答案 1 :(得分:1)
如果您要保存并显示特定数量的记录,可以使用共享首选项本身,如下所示:
public class Highscore {
private SharedPreferences preferences;
private String names[];
private long score[];
public Highscore(Context context)
{
preferences = context.getSharedPreferences("Highscore", 0);
names = new String[10];
score = new long[10];
for (int x=0; x<10; x++)
{
names[x] = preferences.getString("name"+x, "-");
score[x] = preferences.getLong("score"+x, 0);
}
}
public String getName(int x)
{
//get the name of the x-th position in the Highscore-List
return names[x];
}
public long getScore(int x)
{
//get the score of the x-th position in the Highscore-List
return score[x];
}
public boolean inHighscore(long score)
{
//test, if the score is in the Highscore-List
int position;
for (position=0; position<10&&this.score[position]>score;
position+
+);
if (position==10) return false;
return true;
}
public boolean addScore(String name, long score)
{
//add the score with the name to the Highscore-List
int position;
for (position=0; position<10&&this.score[position]>score;
position+
+);
if (position==10) return false;
for (int x=9; x>position; x--)
{
names[x]=names[x-1];
this.score[x]=this.score[x-1];
}
this.names[position] = new String(name);
this.score[position] = score;
SharedPreferences.Editor editor = preferences.edit();
for (int x=0; x<10; x++)
{
editor.putString("name"+x, this.names[x]);
editor.putLong("score"+x, this.score[x]);
}
editor.commit();
return true;
}
}
参考:http://osdir.com/ml/Android-Developers/2010-01/msg00794.html
但是,最好使用SQLite,因为分数可以在获取时进行排序。您可以使用简单的算法
SELECT 'TRUE' WHERE CURRENT_SCORE > MIN(EXISTING_HIGH_SCORES);
DELETE RECORD FROM MY_TABLE WHERE SCORE = MIN(EXISTING_HIGH_SCORES)