SQLite データベースをスコア列の降順で表示することができましたが、この順序で表示されるため、列が int ではなく文字列として認識されていると思います。
7、6、4、2、1、19、
かなり単純なものだと確信していますが、変更方法がわかりません。ここに私の現在のコードがあります:
public class CodeBreakerDB {
public static final String KEY_ROWID = "_id"; //Referencing a specific row in the database.
public static final String KEY_NAME = "player_name";
public static final String KEY_SCORE = "player_score";
private static final String DATABASE_NAME = "PlayerScoresdb"; //Labeling the database
private static final String DATABASE_TABLE = "playerTable"; //Labeling the table
private static final int DATABASE_VERSION = 1;
private DbHelper scoreHelper;
private final Context scoreContext;
private SQLiteDatabase scoreDb;
//databaseHelper to help set up database
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME , null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { //Method only called on first use to generate db.
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_SCORE + " INTEGER);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //Method called after first use
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public CodeBreakerDB(Context c){
scoreContext = c;
}
public CodeBreakerDB open() throws SQLException{
scoreHelper = new DbHelper(scoreContext); //Gets aspects set up for the database including name and version.
scoreDb = scoreHelper.getWritableDatabase(); //Opening of database through scoreHelper.
return this;
}
public void close (){
scoreHelper.close(); //Closes the scoreHelper class.
}
public long createScoreEntry(String name, String score) { // Writing to the actual database.
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_SCORE, score);
return scoreDb.insert(DATABASE_TABLE, null, cv);
}
public String getRank() {
String[] rank = new String[]{ KEY_ROWID };
Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, KEY_SCORE+" DESC");
String rankResult = "";
int iRow2 = c.getColumnIndex(KEY_ROWID); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
rankResult = rankResult + c.getString(iRow2) + "\n";
//Returning value of row that it is currently on.
}
return rankResult; //returning result
}
public String getName() {
String[] name = new String[]{ KEY_NAME };
Cursor c = scoreDb.query(DATABASE_TABLE, name, null, null, null, null, KEY_SCORE+" DESC"); //reading information from db.
String nameResult = "";
int iRow1 = c.getColumnIndex(KEY_NAME); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
nameResult = nameResult + c.getString(iRow1) + "\n";
//Returning value of row that it is currently on.
}
return nameResult; //returning result
}
public String getScore() {
String[] score = new String[]{ KEY_SCORE };
Cursor c = scoreDb.query(DATABASE_TABLE, score, null, null, null, null, KEY_SCORE+" DESC");
String scoreResult = "";
int iRow2 = c.getColumnIndex(KEY_SCORE); //Cursor looking for column setting equal to these ints.
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//Move to first row - where cursor starts and moves to next row as long it is not after last row.
scoreResult = scoreResult + c.getString(iRow2) + "\n";
//Returning value of row that it is currently on.
}
return scoreResult; //returning result
}
}