0

我想创建一个类,HighScore来保存游戏的分数。我使用SQLiteListView显示。这是我的代码:

类 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 时,它会抛出错误。

任何帮助,将不胜感激。谢谢。

4

1 回答 1

0

您需要发布错误,但我看到的第一件事是您没有将主键列名称设置为“_id”。

这是 Android 在几乎任何小部件中使用此数据所必需的。这就是它跟踪数据的方式。您不仅在 DB 中需要它,而且必须调用它,以便它位于用于备份适配器的任何游标中。您不需要显示它,但它必须存在。

于 2012-07-02T02:11:31.767 回答