jeudi 25 février 2016

SQLite Help - How To Display Score On New Activity

I am just starting to learn the principles of android and i am currently making a quiz at the moment to start off. The main issue i have, is when the user selects the correct question, i want the score to be saved in a database, and then retrieved from the database in a TextView on the next activity (question) and so on.

At the moment, it is adding +1 (or -1 for a wrong question) to the count variable, but once it goes to the next activity, it is reset?

I am using a database handler below, and have included my code for question one and question two.

Can someone point me in the right direction?

Thanks for any help offered :)

DatabaseHandler.java

package com.example.pc.quiz;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "game";

// Table name
private static final String TABLE_SCORE = "score";

// Score Table Columns names
private static final String KEY_ID_SCORE = "_id";
private static final String KEY_SCORE = "score_value";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_SCORE_TABLE = "CREATE TABLE " + TABLE_SCORE + "("
            + KEY_ID_SCORE + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_SCORE + " TEXT" + ")";

    db.execSQL(CREATE_SCORE_TABLE);

}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE);

    // Create tables again
    onCreate(db);
}

// Adding new score
public void addScore(int score) {

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_SCORE, score); // score value

    // Inserting Values
    db.insert(TABLE_SCORE, null, values);

    db.close();

}

// Getting All Scores
public String[] getAllScores() {

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_SCORE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list

    int i = 0;

    String[] data = new String[cursor.getCount()];

    while (cursor.moveToNext()) {

        data[i] = cursor.getString(1);

        i = i++;

    }
    cursor.close();
    db.close();
    // return score array
    return data;
}}

AquaQ1.java (Question 1)

 package com.example.pc.quiz;

 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.TextView;
 import android.widget.Toast;

 public class AquaQ1 extends AppCompatActivity {

DatabaseHandler db = new DatabaseHandler(this);

int count;
TextView text1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aqua_q1);

    text1=(TextView)findViewById(R.id.textView1);
    TextView textView = (TextView) findViewById(R.id.textView);

    textView.setText("Score: " + count);
}


public void onClickQ1A(View view) {

    db.addScore(count++);

    Toast.makeText(this, "Correct Answer!", Toast.LENGTH_SHORT).show();

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("Score: " + count);

    startActivity(new Intent(AquaQ1.this, AquaQ2.class));
}

public void onClickQ1B(View view) {
    db.addScore(count--);

    Toast.makeText(this, "Wrong Answer! Try again", Toast.LENGTH_SHORT).show();

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("Score: " + count);
}

public void onClickQ1C(View view) {
    db.addScore(count--);

    Toast.makeText(this, "Wrong Answer! Try again", Toast.LENGTH_SHORT).show();

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("Score: " + count);
}

public void onClickQ1D(View view) {
    db.addScore(count--);

    Toast.makeText(this, "Wrong Answer! Try again", Toast.LENGTH_SHORT).show();

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("Score: " + count);
 }


public void onClickM1(View view) {

    startActivity(new Intent(AquaQ1.this, MainMenu.class));
}
 }

AquaQ2.java (Question 2)

 package com.example.pc.quiz;

 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.TextView;
 import android.widget.Toast;

 public class AquaQ2 extends AppCompatActivity {

DatabaseHandler db = new DatabaseHandler(this);

int count;
TextView text1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aqua_q2);

    text1=(TextView)findViewById(R.id.textView1);
    TextView textView = (TextView) findViewById(R.id.textView);

    textView.setText("Score: " + count);
}


public void onClickQ1A(View view) {

    db.addScore(count++);

    Toast.makeText(this, "Correct Answer!", Toast.LENGTH_SHORT).show();

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("Score: " + count);

    startActivity(new Intent(AquaQ2.this, AquaQ3.class));
}

public void onClickQ1B(View view) {
    db.addScore(count--);

    Toast.makeText(this, "Wrong Answer! Try again", Toast.LENGTH_SHORT).show();

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("Score: " + count);
}

public void onClickQ1C(View view) {
    db.addScore(count--);

    Toast.makeText(this, "Wrong Answer! Try again", Toast.LENGTH_SHORT).show();

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("Score: " + count);
}

public void onClickQ1D(View view) {
    db.addScore(count--);

    Toast.makeText(this, "Wrong Answer! Try again", Toast.LENGTH_SHORT).show();

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText("Score: " + count);
}


public void onClickM1(View view) {

    startActivity(new Intent(AquaQ2.this, MainMenu.class));
}
 }

Aucun commentaire:

Enregistrer un commentaire