1

This is something i cannot figure it out. I had planned to use SQLite Database to save and retrieve every score in a ListView after the player had reach 0 lives as it is an infinite level. But, all the score return me 0. I do not know the issue.

Here is my 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;
}

}
Mobile Developer
  • 191
  • 1
  • 3
  • 10

2 Answers2

3

use hashmap , because Scores scores = new Scores(); always return zero and remove score class

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();
}
Salman500
  • 1,213
  • 1
  • 17
  • 35
  • Omg, it works! Thank you very, very much! Just 2 more question, if i want to retrieve the highest score and display also in the Record Activity. Must i create another one more method in DBHelper class like call public int getHighScore(int highscore)? Also, what is Hashmap about? This one i never really learn before so i do not know the theory and when to implement this method. – Mobile Developer Jul 30 '17 at 10:56
  • Hashmap contain key and value as Hashmap must easy to maintain data for more info visit https://stackoverflow.com/questions/7975802/when-to-use-hashmap-over-linkedlist-or-arraylist-and-vice-versa https://www.tutorialspoint.com/java/java_hashmap_class.htm – Salman500 Jul 30 '17 at 11:02
  • i did not understand your first question – Salman500 Jul 30 '17 at 11:05
  • I mean, if i wish to retrieve the highest score and display it in the Record Activity, how would i do that? Just simply create another method in DBHelper? – Mobile Developer Jul 30 '17 at 11:08
  • I have another question. I had plan to use SearchView so it will be easier for the user to search based on ID. However, it did not work – Mobile Developer Jul 30 '17 at 13:31
0

as in your this code you are using finish(), thats mean that this is end up your current activity and go to previous activity , i am guessing that your previous activity will be Record Activity , this will not run onCreate() second time

use onStart() on Register Activity or use intent instead of finish() i would say use intent

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);


                        }

// Register Activity

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();
    }
Salman500
  • 1,213
  • 1
  • 17
  • 35