我正在进行Android测验。我使用SQLite存储问题和答案。它有3个难点:简单,中等和困难。我需要帮助存储每个难度中得分的用户。我想将分数存储在另一项名为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();
}
}
答案 0 :(得分:0)
您可以在SQLite数据库中创建另一个表(分数表),您可以在其中存储每个测验的用户分数,并使用该表来获取要在其中显示的结果另一项活动。
您可以使用以下狙击来定义得分表:
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);