2017-07-30 4 views
0

これはわかりません。私はSQLiteデータベースを使用して、無限レベルなのでプレイヤーが0になった後、ListViewのすべてのスコアを保存して取得する予定でした。しかし、すべてのスコアは私に0を返す。私は問題を知らない。ここでSQLiteデータベースListViewでスコアを保存して取得しますが、スコアは0です

は)あなたが(仕上げを使用しているあなたのこのコードのように

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

を返しています。私はそれをleft = 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つの質問。コールpublic int getHighScore(int highscore)のようにDBHelperクラスでもう1つのメソッドをもう1つ作成する必要がありますか?また、Hashmap とは何ですか?この1つ私は本当に前に学ぶことはないので、私は理論とこのメソッドを実装するときを知りません。 –

+0

ハッシュマップには、ハッシュマップとしてのキーと値が含まれていますは、詳細情報を参照するためにデータを簡単に維持する必要があります。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

、私のDBHelperで、これはあなたの前のことをあなたの現在の活動を終了し、以前の活動に行き、私は推測していていることをthatsの意味活動が録音活動になります、これは私が言うの登録活動に()二度目

使用ONSTARTを()のonCreateを実行したり、意思の代わりに、仕上げ() を使用しません使用の意図

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

あなたは以前の活動であるRegisterActivity – Salman500

関連する問題