2017-02-26 99 views
-1

我想向我的遊戲添加前10名高分。高分是由只有兩件事 - 得分和難度,但到目前爲止,我不很瞭解如何數據庫的作品,但後幾個教程我有這個做爲什麼這個SQLite數據庫不能正常工作

public class DBHandler extends SQLiteOpenHelper { 

private static final int DATABASE_VERSION = 1; 

private static final String DATABASE_NAME = "highscores"; 

private static final String TABLE_DETAIL = "scores"; 

private static final String KEY_ID = "id"; 
private static final String KEY_TIME = "time"; 
private static final String KEY_DIFFICULTY = "difficutly"; 


public DBHandler(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION);} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_HIGHSCORES_TABLE = "CREATE TABLE " + TABLE_DETAIL + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY, " 
      + KEY_TIME + " TEXT, " 
      + KEY_DIFFICULTY + " TEXT)"; 
    db.execSQL(CREATE_HIGHSCORES_TABLE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAIL);  

    onCreate(db);           
} 
// Adding new score 
public void addScore(int score) { 

    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 

    values.put(KEY_TIME, score); // score value 

    // Inserting Values 
    db.insert(TABLE_DETAIL, null, values); 

    db.close(); 

} 

// Getting All Scores 
public String[] getAllScores() { 

    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_DETAIL; 

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

這裏是類我希望當我打開的頁面與高的分數,在應用程序來控制從

public class highscores extends Activity {  

private ListView scorebox; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    DBHandler db = new DBHandler(this); 
    scorebox = (ListView) findViewById(R.id.scorebox); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.highscores); 
    Log.d("Insert: ", "Inserting .."); 
    db.addScore(9000); 
    Log.d("Reading: ", "Reading all contacts.."); 
} 
} 

數據庫 - 它是空白,如何讓它顯示的東西,我想用這個命令db.addScore(9000);但它不工作。也許我沒有告訴數據庫在哪裏顯示數據?

+0

'String CREATE_HIGHSCORES_TABLE =「CREATE TABLE」+ TABLE _DETAIL +「(」'你在'TABLE'後錯過**空間** –

+0

我添加了空格,但沒有太大改變,頁面打開但又是空白 – Mik

+0

如果你想要插入一些數據獲取一些數據。 –

回答

0

編輯高分類

public class highscores extends Activity {  
    private ListView scorebox; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.highscores); 
    DBHandler db = new DBHandler(this); 
    scorebox = (ListView) findViewById(R.id.scorebox); 
    Log.d("Insert: ", "Inserting .."); 
    db.addScore(9000); 
    Log.d("Reading: ", "Reading all contacts.."); 
    ArrayList<String>ar=new ArrayList<>(); 
    ar=db.getAllScores(); 
    ArrayAdapter<String>ar=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s); 
    scorebox.setAdapter(ar); 
} 
} 

現在你DBHandler類編輯getAllScores()方法像這樣

public ArrayList getAllScores() { 

// Select All Query 
String selectQuery = "SELECT * FROM " + TABLE_DETAIL; 

SQLiteDatabase db = this.getWritableDatabase(); 
Cursor cursor = db.rawQuery(selectQuery, null); 

// looping through all rows and adding to list 


ArrayList<String>data=new ArrayList<>(); 

while (cursor.moveToNext()) { 

    data.add(cursor.getString(1)); 


} 
cursor.close(); 
db.close(); 
// return score array 
return data; 
} 
+0

謝謝你寫的,它終於工作了,最初我在'ArrayAdapter ar = new ArrayAdapter (this,android.R.layout.simple_list_item_1,s)出現錯誤;'說ar已經被使用,並且找不到s,但是我設法處理這些我自己 – Mik

0

如果你正在使用模擬器,你可以拉數據庫文件,並檢查是否創建該表與否,如果您使用的設備,您可以使用此命令

CD/DD:\的Android \ SDK \平臺-tools // Android SDK中路徑

ADB -d殼

運行作爲com.pkg.pkgname

貓/data/data/com.pkg.pkgname/databases/highscores>/SD卡/高分

它會將你的數據庫文件複製到設備的SD卡。使用Sqlite browser你可以打開數據庫文件並檢查表是否被創建。 也將卸載應用程序,並重新安裝

+0

我使用模擬器,我檢查,數據庫確實被創建。我看到高分,高分 - 數據文件夾中的日記文件 – Mik

0

使用此列出你的分數在你的列表視圖

public class highscores extends Activity {  

    private ListView scorebox; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.highscores); 
     DBHandler db = new DBHandler(this); 
     scorebox = (ListView) findViewById(R.id.scorebox); 
     Log.d("Insert: ", "Inserting .."); 
     db.addScore(9000); 
     Log.d("Reading: ", "Reading all contacts.."); 
     String []s=db.getAllScores(); 
     ArrayAdapter<String>ar=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s); 
     scorebox.setAdapter(ar); 
    } 
    } 
+0

我收到一個錯誤:java.lang.NullPointerException:嘗試調用虛方法'java.lang.String java.lang.Object.toString()'對空引用 – Mik

+0

後全logcat的 –

+0

或使用ArrayList,而不是字符串 –

相關問題