Learn, Share, Build

160
October 01, 2017, at 05:12 AM

I have a game which stores it's high scores in a SQLite database but it only stores the scores when I open the window in my app which shows the said scores, if I play 2 games in a row and then open the high scores window it will only show the later score. So I tried to make a method which I can call from the main class but I get null reference error. This below is my Highscore class which saves the scores. How can I make it so it saves the scores even without opening "highscores" window ?

public class highscores extends Activity {
private ListView scorebox1;
private ListView scorebox2;
private ListView scorebox3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.highscores);
    DBHandler db = new DBHandler(this);
    scorebox1 = (ListView) findViewById(R.id.scorebox1);
    scorebox2 = (ListView) findViewById(R.id.scorebox2);
    scorebox3 = (ListView) findViewById(R.id.scorebox3);

    // Create database helper
    DBHandler mDbHelper = new DBHandler(this);
    // Gets the database in write mode
    SQLiteDatabase db2 = mDbHelper.getWritableDatabase();
    // Create a ContentValues object where column names are the keys,
    // and pet attributes from the editor are the values.
    if(difficutlyFrSQL != ""){
    ContentValues values = new ContentValues();                                 
    values.put(DBHandler.KEY_TIME, difficutlyFrSQL);                            
    values.put(DBHandler.KEY_DIFFICULTY, difficutlyFrSQL2);
    long newRowId = db2.insert(DBHandler.TABLE_DETAIL, null, values);           
    // Show a toast message depending on whether or not the insertion was successful
    if (newRowId == -1) {
        // If the row ID is -1, then there was an error with insertion.
        Toast.makeText(this, "Error with saving test score", Toast.LENGTH_SHORT).show();
    } else {
        // Otherwise, the insertion was successful and we can display a toast with the row ID.
        Toast.makeText(this, "Test score saved with row id: " + newRowId, Toast.LENGTH_SHORT).show();
    }
        difficutlyFrSQL = "";
    }
    Log.d("Reading: ", "Reading all scores..");                                                                         
    ArrayList<String>ar3=new ArrayList<>();                                                                             
    ar3=db.getAllScores(3);
    Collections.sort(ar3);
    Collections.reverse(ar3);
    ArrayAdapter<String>ap3=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ar3);
        scorebox1.setAdapter(ap3);
    ArrayList<String>ar2=new ArrayList<>();
    ar2=db.getAllScores(2);
    Collections.sort(ar2);
    Collections.reverse(ar2);
    ArrayAdapter<String>ap2=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ar2);
    scorebox2.setAdapter(ap2);
    ArrayList<String>ar1=new ArrayList<>();
    ar1=db.getAllScores(1);
    Collections.sort(ar1);
    Collections.reverse(ar1);
    ArrayAdapter<String>ap1=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ar1);
    scorebox3.setAdapter(ap1);
}
}
READ ALSO
Learn, Share, Build

Learn, Share, Build

I have a Test Android application and have added a cloud-endpoints backend module to itI have also been able to create a custom class and generate the cloud endpoints for that class

144
Learn, Share, Build

Learn, Share, Build

I am having a problem with 2 rectangles intersection (one is bigger than other) in my android projectI used Rect

147
Learn, Share, Build

Learn, Share, Build

I am new to Nodejs and I have an application that I have deployed to Heroku with a MySQL database

162