Reputation: 1042
I have a "true or false" type of quiz in making. So, two buttons, one true one false, and textview with question. I have an sqlite database imported in Assets folder. 4 columns in it: _id, question, correctAnswer, wrongAnswer. Every column INTEGER except question which is TEXT. So, for every correctAnswer column I set 0 or 1, depending if the answer is true or false. But, in the game, whatever the question is I get Wrong on my left TRUE button, and Correct on my right FALSE button. I don't know what I am doing wrong. Anyway, here's the code:
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) {
}
}
Upvotes: 0
Views: 140
Reputation: 22342
I'm going to make some assumptions, based on this block of code:
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));
}
When c.getInt(2)==1
is true...
labels
, so labels[0]=true
, and labels[1]=false
tacno
to true
(index 0) and netacno
to false
(index 1)When c.getInt(2)==1
is false...
labels
, so labels[0]=false
, and labels[1]=true
netacno
to false
(index 0) and tacno
to true
(index 1)In both cases, tacno
is true(correct) and netacno
is false(incorrect). You did the flip twice.
Upvotes: 1
Reputation: 3425
I'm not 100 percent clear, but it seems to me that when you come back into the view, any old true/fals buttons that are showing on your screen, need to be updated with the value from the database, otherwise they will show as their default setting in the xml layout.
Upvotes: 0