java - SQLite 帮助 - 如何显示新 Activity 的分数

标签 java android sqlite

我刚刚开始学习 android 的原理,目前正在做一个测验来开始。我遇到的主要问题是,当用户选择正确的问题时,我希望将分数保存在数据库中,然后在下一个 Activity (问题)的 TextView 中从数据库中检索,依此类推。

目前,它正在向计数变量添加+1(如果问题错误则为-1),但是一旦进入下一个 Activity ,它就会重置?

我正在使用下面的数据库处理程序,并包含问题一和问题二的代码。

有人能指出我正确的方向吗?

感谢您提供的任何帮助:)

编辑: - 在进一步研究问题后,我意识到将分数添加到数据库的问题应该设置为:

db.addScore(1); 

用正确答案代替

db.addScore(count++);

我现在遇到的唯一问题是我不知道如何在 TextView 中显示数据库中的分数?

textView.setText("Score: " ); 

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(问题 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(问题 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));
}
 }

最佳答案

您使用的 count 值为未初始化的值,这就是为什么您在 onCreate 查询数据库中看到 0 并编写如下方法 getCurrentScore 查询分数列下的值以获取您当前的分数。

与此类似的内容

public String getScore() {
    SQLiteDatabase db = getReadableDatabase();
    String query = "SELECT SCORE FROM " + TABLE_SCORE
    Cursor c = db.rawQuery(query, null)
    if (c.moveToFirst()) {
        count = c.getString(c.getColumnIndex("SCORE"));

    }
    c.close();
    db.close();

    return count;
}

关于java - SQLite 帮助 - 如何显示新 Activity 的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35638158/

相关文章:

android - Android Studio 生成 JavaDoc 时出错

java - 如何使用 Gmail 帐户或服务器或提取文件来为 Android 应用程序中的笔记创建备份?

java - 带有 LinkedList 的 foreach 循环如何运行?

gradle 2.0.0-beta6 的 Android Studio 错误

java - 在 JSTL 中添加不同的时间

android - Room - LiveData 观察者在数据库更新时不会触发

PHP-SQLite 与 SQLite3

android - "table or subquery expected, got "Android Room 错误

java - 使用 "allowedTypes"参数限制struts2中的文件类型

java - RecyclerView 中的背景颜色变化不一致?