Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5935259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:13:44+00:00 2026-05-22T15:13:44+00:00

On completing my Android game I want the user to compare his/her score with

  • 0

On completing my Android game I want the user to compare his/her score with the high score. To do this I stored the current high score in a SQLite database. But I think my method (which seems to work) is clumsy and ugly:

//in the final score activity, which has been sent the users score from the game.
if (extras != null){
            //get users score
        SCORE = extras.getLong("Score");
            //create DB if necessary, otherwise open
        startDatabase();

    /tv is a text view (defined outside this code snippet)
    tv.setText("Your score: "+SCORE+"  \n"+"Top Score: "+ getHighScore());

    }
}

//opens or creates a DB. If it is created insert current score.
public void startDatabase(){
     SQLiteDatabase myDB = null;


     try{    
         myDB = this.openOrCreateDatabase(DB_NAME, 0,null);

         myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                 + SCORE_TABLE
                 + " (key VARCHAR," 
                 + " score NUM)");


     }catch(SQLException e){
         myDB.close();
        }


     Cursor c1 = myDB.rawQuery("SELECT * FROM "+ SCORE_TABLE ,null);

     c1.moveToNext();


    Long HIGHSCORE=0l;

            //try to get current high score.
    try{
    HIGHSCORE = c1.getLong(c1.getColumnIndex("score"));
    }

            //DB score is empty. Fill it with current score. This is the initial high                    `               //score.
    catch(IndexOutOfBoundsException e){
         myDB.execSQL("INSERT INTO "+ SCORE_TABLE+"(score)                        
                                            VALUES('"+SCORE+"')" );
         myDB.close();

    }

    c1.close();
    myDB.close();

}

The next method retrieves the current high score, and if necessary inputs a new highscore.

//returns the high score. also inputs new score as high score if it is high enough.
public long getHighScore(){
    SQLiteDatabase myDB = null;

    myDB = this.openOrCreateDatabase(DB_NAME, 0,null);

    Cursor c1 = myDB.rawQuery("SELECT * FROM "+ SCORE_TABLE ,null);

    c1.moveToNext();

    Long HIGHSCORE=0l;
    try{
    HIGHSCORE = c1.getLong(c1.getColumnIndex("score"));
    }
    catch(IndexOutOfBoundsException e){



    }

    //if user score is higher than high score...
     if(HIGHSCORE<=SCORE){
         myDB.execSQL("UPDATE "+ SCORE_TABLE+"  SET score= '"+SCORE+"'" );
         HIGHSCORE=SCORE;
         myDB.close();
     }

    c1.close();
    myDB.close();
    return HIGHSCORE;
}

I dont particularly like the code. Initially I thought doing the ifnal score/high score comparison would be a piece of cake, even with my basic knowledge of SQL. I think I made a mountian out of a molehill with this. Is there a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:13:44+00:00Added an answer on May 22, 2026 at 3:13 pm

    Well it appears you are only saving the highest score – and not all the scores, or even the top n scores. If this is the case, it is probably easier to just save the high score in a sharedpreference

    public long getHighScore(){
    SharedPreferences pp = PreferenceManager
                .getDefaultSharedPreferences(context);
    
    
    if(score>pp.getLong("highscore",0l)){
        Editor pe=(Editor) pp.edit();
        pe.putLong("highscore",score);
        pe.commit();
        return score;
     } else {return pp.getLong("highscore",0l);}
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to android and i want to make some animation (simple game
I'm getting an Unable to destroy activity xxx: android.database.sqlite.SQLiteException: unable to close due to
i am implementing puzzle game app in android . in this application one level
I was working on my android project. After completing some coding, I imported remaining
I want users to receive 'points' for completing various tasks in my application -
I'm looking for a more efficient way of completing this task. I need to
I'm having a pretty nasty problem with an auto-completing textbox. I want to initiate
I am completing an Android program to classify the genre of a song on
I am new to android, I am in a completing stage in creating one
I am new to Android. In my application I want to add an process

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.