2017-07-30 56 views
0

這是我無法弄清楚的。我曾計劃使用SQLite數據庫在玩家達到0次生命後保存並檢索ListView中的每個分數,因爲它是無限級別。但是,所有的分數都返回給我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; 
} 

} 
+0

你在哪裏初始化代碼中的生活?我猜這就是問題所在,因爲它總是返回相同的分數,即0 – Mandy8055

+1

我把它放在全局像左= 10;即使得分也是相同的,但我只初始化爲0 –

+1

我應該怎麼知道你是零,發完整的代碼 – Salman500

回答

2

使用散列映射,因爲成績得分=新分數();總是返回零,並刪除比分類

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(); 
} 
+0

Omg,它的工作!非常非常感謝你!還有2個問題,如果我想檢索最高分並且還在記錄活動中顯示。我必須在DBHelper類中創建另一個更多方法,如調用public int getHighScore(int highscore)?另外,什麼是Hashmap about?這一個我從來沒有真正學過,所以我不知道理論和何時實施這種方法。 –

+0

Hashmap包含key和value作爲Hashmap 必須易於維護數據以獲取更多信息訪問https://stackoverflow.com/questions/7975802/when-to-use-hashmap-over-linkedlist-or-arraylist-and-vice-如果我希望檢索最高分並將其顯示在記錄活動中,我會如何處理這個問題 https://www.tutorialspoint.com/java/java_hashmap_class.htm – Salman500

+0

我不明白你的第一個問題 – Salman500

0

在你的代碼使用的是結束(),那意味着這是結束當前的活動,並轉到以前的活動,我猜測,你以前的活動將記錄活動,這不會對註冊活動運行的onCreate()第二次

使用在onStart(),或使用意圖,而不是結束() 我想說的使用意圖

if (left == 0) { 
        AlertDialog.Builder builder = new AlertDialog.Builder(Level10.this); 

        builder.setTitle("Game Over"); 
        builder.setMessage("You gain a total score of " + score + "! Click back!"); 
        builder.setCancelable(false); 
        builder.setPositiveButton("Back", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dbHelper.addHighScore(score); 
          dbHelper.close(); 
          //finish(); 
       Intent i = new Intent(this,RegisterActivity.class); 
       startActivity(i); 


         } 

//註冊活動

ListView lv; 
ArrayAdapter aa; 
ArrayList<Scores> al; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_score_records); 

    setList(); 
} 



    @Override 
    protected void onStart() { 
     super.onStart(); 

     setList(); 
    } 


    public void setList() 
    { 
     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(); 
    } 
+0

它仍然給0 ... –

+0

你以前的活動是註冊活動 – Salman500