Android 如何保存分数
我正在做一个机器人测试。我使用SQLite存储问题和答案。它有三个困难:简单、中等和困难。我需要帮助存储用户在每个困难中得到的分数。我想将分数存储在另一个名为“分数”的活动中,如Android 如何保存分数,android,Android,我正在做一个机器人测试。我使用SQLite存储问题和答案。它有三个困难:简单、中等和困难。我需要帮助存储用户在每个困难中得到的分数。我想将分数存储在另一个名为“分数”的活动中,如Easy 5/5 Medium 5/5 Hard 5/5 这是我的数据库: public class DbHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static fina
Easy 5/5 Medium 5/5 Hard 5/5
这是我的数据库:
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "QUIZ";
private static final String TABLE_QUIZ = "quiz";
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer";
private static final String KEY_OPTA = "opta";
private static final String KEY_OPTB = "optb";
private static final String KEY_OPTC = "optc";
private static final String KEY_OPTD = "optd";
private static final String q_level = "level";
private static final String QuestionNumber = "q_number";
private SQLiteDatabase dbase;
public String question, optionA, optionB, optionC, optionD, answer;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase = db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUIZ + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
+ KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT, " + KEY_OPTD +" TEXT, " + q_level + " TEXT, " + QuestionNumber + " INTEGER)";
db.execSQL(sql);
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','EASY','1')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q2','A1','A2','A3','A4','Answer','EASY','2')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q3','A1','A2','A3','A4','Answer','EASY','3')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q4','A1','A2','A3','A4','Answer','EASY','4')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q5','A1','A2','A3','A4','Answer','EASY','5')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','MEDIUM','1')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','MEDIUM','2')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','MEDIUM','3')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','MEDIUM','4')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','MEDIUM','5')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','HARD','1')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','HARD','2')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','HARD','3')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','HARD','4')");
db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
"'Q1','A1','A2','A3','A4','Answer','HARD','5')");
addQuestions();
//db.close();
}
private void addQuestions() { }
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUIZ);
onCreate(db);
}
public void getQuestByLevel(String level, int qnum) {
String selectQuery = "SELECT * FROM " + TABLE_QUIZ + " WHERE "+ q_level +" = '"+ level +"' ORDER BY RANDOM() LIMIT 5;";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
question=cursor.getString(cursor.getColumnIndex(KEY_QUES));
optionA=cursor.getString(cursor.getColumnIndex(KEY_OPTA));
optionB=cursor.getString(cursor.getColumnIndex(KEY_OPTB));
optionC=cursor.getString(cursor.getColumnIndex(KEY_OPTC));
optionD=cursor.getString(cursor.getColumnIndex(KEY_OPTD));
answer=cursor.getString(cursor.getColumnIndex(KEY_ANSWER));
} while (cursor.moveToNext());
}
}
}
问答活动:
public class QuizActivity extends Activity {
int score = 0;
int qnum = 1;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button butNext;
RadioGroup rdgrp;
String corAnswer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_quiz);
DbHelper db = new DbHelper(this);
rdgrp = (RadioGroup) findViewById(R.id.questionAndAnswers);
txtQuestion = (TextView) findViewById(R.id.textView1);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
rdd = (RadioButton) findViewById(R.id.radio3);
butNext = (Button) findViewById(R.id.button1);
corAnswer = "";
onCreateQuestion();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
public void onCreateQuestion() {
String level = getIntent().getExtras().getString("level");
DbHelper db = new DbHelper(this);
db.getQuestByLevel(level, qnum);
txtQuestion.setText(db.question);
rda.setText(db.optionA);
rdb.setText(db.optionB);
rdc.setText(db.optionC);
rdd.setText(db.optionD);
corAnswer = db.answer;
qnum++;
}
public void onClickNext(View view) {
String level = getIntent().getExtras().getString("level");
DbHelper db = new DbHelper(this);
db.getQuestByLevel(level, qnum);
RadioGroup grp = (RadioGroup) findViewById(R.id.questionAndAnswers);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
if (answer == null) {
Toast.makeText(QuizActivity.this, "select an answer please", Toast.LENGTH_SHORT).show();
return;
}
if (corAnswer!= null && corAnswer.equalsIgnoreCase((String) answer.getText())) {
score++;
Log.d("answer", "Your score" + score);
}
if (qnum <= 5) {
} else {
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score);
intent.putExtras(b);
startActivity(intent);
finish();
}
txtQuestion.setText(db.question);
rda.setText(db.optionA);
rdb.setText(db.optionB);
rdc.setText(db.optionC);
rdd.setText(db.optionD);
corAnswer = db.answer;
qnum++;
rdgrp.clearCheck();
}
}
公共类quizaActivity扩展活动{
智力得分=0;
int qnum=1;
文本视图TXT问题;
单选按钮rda、rdb、rdc、rdd;
按钮butNext;
放射组rdgrp;
字符串corAnswer=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
此.setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u横向);
setContentView(R.layout.activity\u测验);
DbHelper db=新的DbHelper(this);
rdgrp=(放射组)findViewById(R.id.问卷和回答);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
rdd=(RadioButton)findViewById(R.id.radio3);
但是下一步=(按钮)findViewById(R.id.button1);
corAnswer=“”;
onCreateQuestion();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.activity\u测验,菜单);
返回true;
}
public void onCreateQuestion(){
字符串级别=getIntent().getExtras().getString(“级别”);
DbHelper db=新的DbHelper(this);
db.getQuestByLevel(level,qnum);
txtQuestion.setText(db.question);
rda.setText(db.optionA);
rdb.setText(db.optionB);
rdc.setText(db.optionC);
rdd.setText(db.optionD);
corAnswer=db.answer;
qnum++;
}
公共void onClickNext(视图){
字符串级别=getIntent().getExtras().getString(“级别”);
DbHelper db=新的DbHelper(this);
db.getQuestByLevel(level,qnum);
放射组grp=(放射组)findViewById(R.id.问题和回答);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
如果(答案==null){
Toast.makeText(quizaActivity.this,“请选择答案”,Toast.LENGTH_SHORT.show();
返回;
}
if(corAnswer!=null&&corAnswer.equalsIgnoreCase((字符串)answer.getText()){
分数++;
Log.d(“回答”、“你的分数”+分数);
}
如果(qnum,您可以在SQLite数据库中创建另一个表(分数表)),您可以在该表中存储每个测验的用户分数,并使用该表获取要在其他活动中显示的结果
您可以使用以下snipe定义分数表:
private static final String TABLE_SCORE= "score";
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_SCORE + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUIZ_ID + "INTEGER, " + SCORE + " INTEGER)";
db.execSQL(sql);
在给出的代码中没有问题。我只需要帮助如何保存用户在每个困难的测验中获得的分数为每个困难创建不同的表使用SharedReferences
或SQLite数据库
。嗯,我在调用每个困难的分数时遇到问题。你能帮助我如何调用它吗?