Reputation: 11
I'm trying to create a quiz test. In the questions form I make 3 buttons that when the user click the answer. It restarts the same layout and the java class, so it appears the next question. Well this way is not so good, as it's painful to call the same layout again and again. So how I change the code so it change the questions in the same layout 20 times? I tried while
function but it does not work as it dont wait to press a button to continue to next question (if there is a command to wait from user to make an action to continue the while loop, it would work). Here is my code:
package dv.qtest.dvyzual;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class startGame extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startgame);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.correct);
AnyDBAdapter eventsData = new AnyDBAdapter(getBaseContext());
eventsData.open();
myMenu.question = myMenu.question +1;
String qq = getRandomNumber();
Cursor c = eventsData.ExampleSelect(qq);
this.startManagingCursor(c);
eventsData.close(); final String h = c.getString(5); setQuestions(c);
final Button r1 = (Button)findViewById(R.id.b1);
r1.setOnClickListener (new View.OnClickListener() { public void onClick(View v) {
if ("1".equals(h)) {
r1.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 1;
mp2.start();
}
else {
r1.setBackgroundResource(R.drawable.buttonneowrong);
}
finish();
if (myMenu.question == 20) {
startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.ENDQUIZGAME"));}
else {
startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.STARTQUIZGAME"));
}
} }); final Button r2 = (Button)findViewById(R.id.b2);
r2.setOnClickListener (new View.OnClickListener() { public void onClick(View v) {
if ("2".equals(h)) {
r2.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 1;
}
else {
r2.setBackgroundResource(R.drawable.buttonneowrong);
}
finish();
if (myMenu.question == 20) {startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.ENDQUIZGAME"));}
else {startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.STARTQUIZGAME"));} } });
final Button r3 = (Button)findViewById(R.id.b3);
r3.setOnClickListener (new View.OnClickListener() { public void onClick(View v) {
if ("3".equals(h)) {
r3.setBackgroundResource(R.drawable.buttonneocorrect);
myMenu.score = myMenu.score + 1;
mp2.start();
}
else {
r3.setBackgroundResource(R.drawable.buttonneowrong);
}
finish();
if (myMenu.question == 20) {startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.ENDQUIZGAME"));}
else {startActivity(new Intent("dv.qtest.dvyzual.QuizTestActivity.STARTQUIZGAME"));} } });
}
private String getRandomNumber(){
Random generator = new Random();
int n = 49;
n = generator.nextInt(n) +1;
String n1 = ""+n;
return n1;
}
private void setQuestions(Cursor c) {
TextView rscore = (TextView)findViewById(R.id.q1); rscore.setText("Score: " + myMenu.score + " Question: "
+ myMenu.question);
TextView r0 = (TextView)findViewById(R.id.textView1); r0.setText(c.getString(1));
Button r1 = (Button)findViewById(R.id.b1); r1.setText(c.getString(2));
Button r2 = (Button)findViewById(R.id.b2); r2.setText(c.getString(3));
Button r3 = (Button)findViewById(R.id.b3); r3.setText(c.getString(4));
} }
Upvotes: 1
Views: 788
Reputation: 1065
Well I won't response your answer... because you are in the wrong way to start Android development.
You'll have to learn some concepts of OOP... read in order :
http://www.osnews.com/story/6788
http://docs.oracle.com/javase/tutorial/java/concepts/
And also this to have a good code implementation :
http://source.android.com/source/code-style.html
Then... look the android samples, there are very good samples, they are in :
%AndroidSDK%/samples/
Also, you will have to understand the android architecture and the android running :
Upvotes: 1