2012-07-02 66 views
0

我想創建一個課程HighScore以保存遊戲的分數。我使用SQLiteListView顯示。這裏是我的代碼:使用遊戲的SQLite保存分數

類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; 
} 
} 

類高分:

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

[如何接受答案](http://meta.stackexchange.com/a/65088) – Lucifer

回答

0

您需要發佈錯誤,但我看到的第一件事是您沒有將主鍵列名稱設置爲「_id」。

這是Android在幾乎所有小部件中使用此數據所必需的。它是如何跟蹤數據的。您不僅需要在數據庫中使用它,而且必須調用它,以便用於備份適配器的任何光標。你不需要顯示它,但它必須在那裏。