이것은 내가 이해할 수없는 것입니다. 플레이어가 무한 레벨이기 때문에 생명이 0에 도달 한 후 SQLite 데이터베이스를 사용하여 ListView의 모든 점수를 저장하고 검색 할 계획이었습니다. 하지만 모든 점수는 0을 반환합니다. 문제를 모르겠습니다.
여기 내 DBHelper가 있습니다.
public class DBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "highscore.db";
private static final String TABLE_HIGHSCORE = "highscore";
private static final String COLUMN_ID = "id";
private static final String COLUMN_SCORE = "score";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableSql = "CREATE TABLE " + TABLE_HIGHSCORE + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_SCORE + " INTEGER " + ");";
db.execSQL(createTableSql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_HIGHSCORE);
onCreate(db);
}
public void addHighScore(int highscore) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_SCORE, highscore);
db.insert(TABLE_HIGHSCORE,null,values);
db.close();
}
public ArrayList<Scores> getAllScores() {
ArrayList<Scores> scoresList = new ArrayList<>();
String selectQuery = "SELECT " + COLUMN_ID + ", " + COLUMN_SCORE + " FROM " + TABLE_HIGHSCORE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(0);
int score = cursor.getInt(1);
Scores scores = new Scores(id,score);
scoresList.add(scores);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return scoresList;
}
}
Scores scores = new Scores (); 때문에 hashmap을 사용하십시오 . 항상 0을 반환하고 점수 클래스를 제거합니다.
public ArrayList<HashMap<String,Integer>> getAllScores() {
ArrayList<HashMap<String,Integer>> scoresList = new ArrayList<>();
String selectQuery = "SELECT " + COLUMN_ID + ", " + COLUMN_SCORE + " FROM " + TABLE_HIGHSCORE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String,Integer> hashmap = new HashMap<>();
int id = cursor.getInt(0);
int score = cursor.getInt(1);
hashmap.put("score",score);
hashmap.put("id",id);
scoresList.add(hashmap);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return scoresList;
}
public class ScoreAdapter extends ArrayAdapter<HashMap<String,Integer>> {
Context context;
ArrayList<HashMap<String,Integer>> alScore;
int resource;
TextView tvScore;
public ScoreAdapter(Context context, int resource, ArrayList<HashMap<String,Integer>> scores) {
super(context, resource, scores);
this.context = context;
this.resource = resource;
this.alScore = scores;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.scorerow, parent, false);
tvScore = (TextView)view.findViewById(R.id.scores);
HashMap<String,Integer> hashmap = alScore.get(position);
tvScore.setText("Score: " + String.valueOf(hashmap.get("score"));
return view;
}
ListView lv;
ArrayAdapter aa;
ArrayList<HashMap<String,Integer>> al;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score_records);
lv = (ListView)findViewById(R.id.lvScoreRecord);
DBHelper db = new DBHelper(ScoreRecords.this);
al = db.getAllScores();
aa = new ScoreAdapter(this, R.layout.scorerow, al);
lv.setAdapter(aa);
aa.notifyDataSetChanged();
db.close();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다