Search This Blog

Tuesday 16 October 2012

Presenting data from a database to a user

We have spent the last few posts looking at the class which encapsulates all the database related activity.  In the case of my Maths Test App this class is the MathsTestDbAdapter class (By the way my Maths Test App can be downloaded by searching Google Play for 'Sundev Pabla').

Today we will look at how you would use this class in the main part of your app.  You would use this class to get data from the database and put data into the database.  In my Maths Test App I use a database to store top scores.  In order to present top scores to the user I need to read top score info from the database and present it to the user.

In the Maths Test App I have a separate activity which I use for displaying scores to the user.  In the OnCreate( ) function of this activity I instantiate an instance of the MathsTestDbAdapter class and then call the open function on the instance.


        mDbHelper = new MathsTestDbAdapter(this);
        mDbHelper.open();

I can now use the mDbHelper object to call the get data functions in the MathsTestDbAdapter class.  I do this in the fillData function of  my ViewScoreActivity class.  The code for the fill data function is shown below:


    private void fillData()
    {

    Cursor topScoreCursor = mDbHelper.fetchScoresBasedOnSpecifiedGameLevelAndType(this.getResources().getStringArray(R.array.gameLevelArray)[gameLevelSpinner2.getSelectedItemPosition()],this.getResources().getStringArray(R.array.gameTypeArray)[gameTypeSpinner2.getSelectedItemPosition()]);
   
    startManagingCursor(topScoreCursor);
       
    tvInitial1 = (TextView) findViewById(R.id.initials1);
    tvScore1 = (TextView) findViewById(R.id.score1);
    tvInitial2 = (TextView) findViewById(R.id.initials2);
    tvScore2 = (TextView) findViewById(R.id.score2);
    tvInitial3 = (TextView) findViewById(R.id.initials3);
    tvScore3 = (TextView) findViewById(R.id.score3);
    tvInitial4 = (TextView) findViewById(R.id.initials4);
    tvScore4 = (TextView) findViewById(R.id.score4);
    tvInitial5 = (TextView) findViewById(R.id.initials5);
    tvScore5 = (TextView) findViewById(R.id.score5);
   
    // Will use switch case without breaks to populate table
    topScoreCursor.moveToFirst();
    switch (topScoreCursor.getCount())
    {
    case 5:
    tvInitial5.setText(""+topScoreCursor.getString(1));
    tvScore5.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    case 4:
    tvInitial4.setText(""+topScoreCursor.getString(1));
    tvScore4.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    case 3:
    tvInitial3.setText(""+topScoreCursor.getString(1));
    tvScore3.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    case 2:
    tvInitial2.setText(""+topScoreCursor.getString(1));
    tvScore2.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    case 1:
    tvInitial1.setText(""+topScoreCursor.getString(1));
    tvScore1.setText(""+topScoreCursor.getDouble(2));
    topScoreCursor.moveToNext();
    break;
    }
    }

The first thing I do in the fillData( ) function is call the fetchScoresBasedOnSpecifiedGameLevelAndType( ) function.  This returns a Cursor object which contains the data extracted from the database.

I then call startManagingCursor( ) function and pass in the cursor object.  This means that Android will look after the Cursor life cycle for me and I don't need to worry about things like closing the cursor when the activity is destroyed.  The moveToFirst( ) function on the Cursor object is used to move to the first data record returned by performing the query on the database.  In the switch statement I then use the Cursor to populate TextViews with data from the database.  For each record the first column or array element is a unique id, the second column is the players initials and the third column is the score.  To move to the next record you call moveToNext( ) on the cursor object.

Hopefully you can follow the code in the fillData( ) function, this is easy for me to say because I wrote the code and it may not be as self explanatory as I thing - so if you have got any questions then please get in touch.  I will leave it here for today, all the best until next time.

No comments:

Post a Comment