Help on Quiz program

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Help on Quiz program

Postby javasubbu » Wed Feb 22, 2012 3:00 pm

Hi,

I am a beginner and I am stuck whilst developing a simple quiz
application in android. The code is below. onClick next button is not
displaying the next random question. Any help?


Code: Select all
package com.quiz;


import java.util.Random;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class Quiz2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Random r = new Random();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final String[] Countries =
{"India","U.S","Singapore","Malaysia","Sri
Lanka","Thailand","Indonesia","China","Japan","Korea","Vietnam"};
        final String[] Capitals = {"Delhi","Washington D.C","Singapore
City","Kaula lampur", "Colombo",
"Bangkok","Jakarta","Beiing","Tokyo","Seoul","Ho Chi Min City"};
        String question;
        RadioGroup rg1 = (RadioGroup)findViewById(R.id.rg1);
        Button but1 = (Button) findViewById(R.id.but1);
        for(int i=0;i<4;i++){
                int num = r.nextInt(10);
                question = "Whats the capital of the country " +
Countries[num];
                TextView Question = (TextView) findViewById(R.id.ques);
                Question.setText(question);
                RadioButton ans1 = (RadioButton) findViewById(R.id.ans1);
                RadioButton ans2 = (RadioButton) findViewById(R.id.ans2);
                RadioButton ans3 = (RadioButton) findViewById(R.id.ans3);
                RadioButton ans4 = (RadioButton) findViewById(R.id.ans4);
                ans1.setText(Capitals[num+1]);
                ans2.setText(Capitals[num]);
                ans3.setText(Capitals[num+2]);
                ans4.setText(Capitals[num-2]);


        }
        but1.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View v) {
                                finish();
                                Bundle tempbundle = new Bundle();
                                onCreate(tempbundle);
                        }
                });
    }



}



I suspect but1.setOnClickListener method.

Regards,
Subbu
javasubbu
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 22, 2012 2:57 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests