我有一个“真假”类型的测验。所以,两个按钮,一个真一个假,一个带问题的文本视图。我在 Assets 文件夹中导入了一个 sqlite 数据库。其中有 4 列:_id、问题、正确答案、错误答案。除了问题是 TEXT 之外,每一列都是 INTEGER。因此,对于每个正确答案列,我设置 0 或 1,具体取决于答案是真还是假。但是,在游戏中,无论问题是什么,我的左侧 TRUE 按钮都会出现错误,而右侧 FALSE 按钮会出现正确。我不知道我做错了什么。无论如何,这是代码:
public class Kviz extends Activity implements OnClickListener{
Button true,false;
TextView question;
LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();
private String generateWhereClause(){
StringBuilder result = new StringBuilder();
for (Long l : mAnsweredQuestions){
result.append(" AND _ID <> " + l);
}
return result.toString();
}
private class Answer {
public Answer(int opt, boolean correct) {
option = opt;
isCorrect = correct;
}
int option;
boolean isCorrect;
}
Runnable mLaunchTask = new Runnable() {
public void run() {
nextQuestion();
}
};
Handler mHandler = new Handler();
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
Answer ans = (Answer) v.getTag();
if (ans.isCorrect) {
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
mHandler.postDelayed(mLaunchTask,1200);
}
else{
Toast.makeText(getApplicationContext(), "Incorrect!", Toast.LENGTH_SHORT).show();
mHandler.postDelayed(mLaunchTask,1200);
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kviz);
inicijalizujVarijable();
nextQuestion();
}
private void nextQuestion() {
TestAdapter mDbHelper = new TestAdapter(this);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
if(!myDbHelper.checkDataBase()){
mDbHelper.createDatabase();
}
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestData(generateWhereClause());
c.moveToFirst();
mAnsweredQuestions.add(c.getLong(0));
List<Answer> labels = new ArrayList<Answer>();
if (c.getInt(2)==1){
labels.add(new Answer(c.getInt(2), true));
labels.add(new Answer(c.getInt(3), false));
tacno.setTag(labels.get(0));
netacno.setTag(labels.get(1));
}else{
labels.add(new Answer(c.getInt(2), false));
labels.add(new Answer(c.getInt(3), true));
netacno.setTag(labels.get(0));
tacno.setTag(labels.get(1));
}
true.setOnClickListener(clickListener);
false.setOnClickListener(clickListener);
}
finally{
mDbHelper.close();
}
}
private void inicijalizujVarijable() {
true = (Button) findViewById(R.id.bTacno);
false = (Button) findViewById(R.id.bNetacno);
question = (TextView) findViewById(R.id.tvPitanje);
}
public void onClick(View v) {
}
}