2017-02-26 167 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,而不是字符串 –