2014-06-23 40 views
0

我正在嘗試在我的遊戲中編寫代碼以在計時器超時後保存分數。我有我的分數,我有我的計時器。我讀了一些關於SharedPreferences和SQLite的內容。我知道我想保存前10名的高分,但SharedPreferences最好只有1分。我試圖圍繞SQLite包裹我的頭,但我無法得到它。任何人都可以幫助我找出一行代碼來保存前10名得分。SQLite或SharedPreferences獲得高分

繼承人我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

有你使用SQLite獲得前10名排行榜在你的應用程序? – dipali

+0

你開始編碼sqlite數據庫..?如果沒有這裏有一個很好的教程http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ –

+0

SQLite'只存儲10位數似乎不可行...你應該堅持到我的意見'SharedPreferences'。 – SMR

回答

4

您可以使用下面給出的代碼保存任意數量的分數並檢索它們。創建一個名爲DatabaseHandler.java的新數據庫輔助類,並在其中添加以下代碼。

要初始化類的活動,把下面一行在你的活動:

DatabaseHandler db = new DatabaseHandler(this); 

然後增加價值DB使用,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; 
} 

}

+0

感謝您的幫助! – androidcoderookie

+0

@androidcoderookie:yw :) –

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);
  • 如果記錄是可用的,和現有的高分> 10進入DB號,然後 DELETE RECORD FROM MY_TABLE WHERE SCORE = MIN(EXISTING_HIGH_SCORES)
  • INSERT當前得分。
+0

謝謝你的幫助! – androidcoderookie