我刚刚开始学习android的原理,目前我正在进行测验以开始。我遇到的主要问题是,当用户选择正确的问题时,我希望将分数保存在数据库中,然后在下一个活动(问题)的TextView中从数据库中检索,依此类推。
目前,它正在向count变量添加+1(或错误的问题为-1),但一旦进入下一个活动,它会被重置?
我正在使用下面的数据库处理程序,并且包含了我的问题1和问题2的代码。
有人能指出我正确的方向吗?
感谢您提供的任何帮助:)
编辑 - 在查看问题之后,我意识到将分数添加到数据库的问题是它应该设置为:
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));
}
}
答案 0 :(得分:1)
您正在使用count
的未初始化值,这就是为什么您在0
查询您的数据库中onCreate
并编写一个查询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;
}
答案 1 :(得分:0)
在我看来,这是因为你的增量。当您对运算符进行后缀处理时,将调用该方法。因此,如果您拨打++counter
而不是counter++
,则应该修复错误。这同样适用于减号。如果您想拥有更多属于增量和减量的示例,请查看here