Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make a loop that will wait an action first to continue, in android?
    text
    copied!<p>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 <code>while</code> 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:</p> <pre><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)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload