我还是Android新手,我还在学习如何使用SQLite。
这是我正在制作的一个函数,用于随机从数据库中获取数据并将其存储在列表中。这里的问题是,当它首次在设备上运行时,我唯一能得到的就是数据库中的最后一行。我不确定我明白这里发生了什么。
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String query = "SELECT * FROM " + TABLE_NAME + ";" ;
dba = this.getReadableDatabase();
Cursor cursor = dba.rawQuery(query, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setOPTA(cursor.getString(2));
quest.setOPTB(cursor.getString(3));
quest.setOPTC(cursor.getString(4));
quest.setANSWER(cursor.getString(5));
quest.setISIMAGE(cursor.getInt(6));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
cursor.close();
dba.close();
return quesList;
}
这是使用函数
的活动public class Testactivity extends AppCompatActivity {
List<Question> quesList;
int score;
int qid = 0;
//static int n;
Question currentQ;
QuizHelper db;
TextView scored, tv_score;
TextView txtQuestion, txtCounter;
Button button1, button2, button3;
public static String ans;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testactivity2);
QuizHelper db = new QuizHelper(this);
quesList = db.getAllQuestions();
currentQ = quesList.get(qid);
this.score = 0;
tv_score = (TextView) findViewById(R.id.score);
tv_score.setText(String.valueOf(score));
txtCounter = (TextView) findViewById(R.id.tv_counter);
db = new QuizHelper(this);
quesList = db.getAllQuestions();
currentQ = quesList.get(qid);
txtQuestion = (TextView) findViewById(R.id.txtQuestion);
img = (ImageView) findViewById(R.id.imgQuestion);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
scored = (TextView) findViewById(R.id.score);
Toast.makeText(getApplicationContext(), "I'm here!", Toast.LENGTH_SHORT).show();
//db.getQuestion();
setQuestionView();
// button click listeners
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// passing the button text to other method
// to check whether the answer is correct or not
// same for all three buttons
getAnswer(button1.getText().toString());
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button2.getText().toString());
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button3.getText().toString());
}
});
}
private void setQuestionView(){
//display control
txtQuestion.setText(currentQ.getQUESTION());
button1.setText(currentQ.getOPTA());
button2.setText(currentQ.getOPTB());
button3.setText(currentQ.getOPTC());
qid++;
}
public void getAnswer(String AnswerString) {
if (currentQ.getANSWER().equals(AnswerString)) {
// if conditions matches increase the int (score) by 1
// and set the text of the score view
score++;
scored.setText(" " + this.score);
Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show();
db.getAllQuestions();
setQuestionView();
//db.getQuestion();
} else {
Intent intent = new Intent(Testactivity.this, result.class);
// if unlucky start activity and finish the game
// Intent intent = new Intent(Testactivity.this, result.class);
Toast.makeText(getApplicationContext(), "WRONG, SORRY!", Toast.LENGTH_SHORT).show();
//db.getQuestion();
// db.getScore(test_entry.username, this.score);
// passing the int value
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
finish();
}
if(qid < 3 ) {
currentQ = quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(Testactivity.this,result.class);
Bundle b = new Bundle();
b.putInt("score", score);
intent.putExtras(b);
startActivity(intent);
finish();
}
}
我的getAllQuestions()函数是否缺少/错误或者我调用错了?
这是我的数据库的实现方式以及数据的插入方式
QuizHelper(数据库助手):
public void onCreate(SQLiteDatabase db) {
//ta = new Testactivity();
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES + " TEXT, "
+ KEY_OPTA + " TEXT, " + KEY_OPTB+ " TEXT, " + KEY_OPTC+ " TEXT, " + KEY_ANSWER
+ " TEXT, " + KEY_ISIMAGE + " INTEGER(11));";
String USER_RECORD = "CREATE TABLE " + TABLE_USER + " (" + USER_NAME + " TEXT, " + USER_SCORE + " INTEGER);";
db.execSQL(CREATE_TABLE);
db.execSQL(USER_RECORD);
addQuestion(db);
}
数据
private void addQuestion(SQLiteDatabase db) {
Question q1 = new Question("A driver who got caught on drugs or alchohol is punishable by", "Suspension of license","Six months of imprisonment","All of the above","All of the above", 0);
this.addQuestiontoDB(q1,db);
Question q2 = new Question("What is the fine for driving without a valid license?", "Php 500.00","Php 500.00 and vehicle impounded","Php 3, 000.00","Php 3, 000.00", 0);
this.addQuestiontoDB(q2,db);
Question q3 = new Question("If you are caught, how many days does it take to fix your case and redeem the license", "30 days","15 days","10 days","15 days", 0);
this.addQuestiontoDB(q3,db);
}
// Adding new question
public void addQuestiontoDB(Question question, SQLiteDatabase db) {
//db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(KEY_ID, Question.ID);
values.put(KEY_QUES, question.getQUESTION());
values.put(KEY_OPTA, question.getOPTA());
values.put(KEY_OPTB, question.getOPTB());
values.put(KEY_OPTC, question.getOPTC());
values.put(KEY_ANSWER, question.getANSWER());
values.put(KEY_ISIMAGE, question.getISIMAGE());
// Inserting Row
db.insert(TABLE_NAME, null, values);
}
如果我遗漏了任何细节,请告诉我。谢谢。