-2

I'm working on a test application. I coded that for now and it's working good for first question. But I don't know how to go second question. I tried to use while-do loop and something else for to do that but didn't work. What can I do? Can anyone help me for that?

public class testing extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testing);

        TextView view = (TextView) findViewById(R.id.soru1);
        final Button answer1 = (Button) findViewById(R.id.answer1);
        final Button answer2 = (Button) findViewById(R.id.answer2);
        final Button answer3 = (Button) findViewById(R.id.answer3);
        final Button answer4 = (Button) findViewById(R.id.answer4);

        final ArrayList<String> questions= new ArrayList<String>();
        countries.add("question1");
        countries.add("question2");
        countries.add("question3");
        countries.add("question4");
        countries.add("question5");
        countries.add("question6");
        countries.add("question7");
        countries.add("question8");

        final int[] answers= new int[]{
                R.drawable.pic1, 
                R.drawable.pic2,
                R.drawable.pic3,
                R.drawable.pic4,
                R.drawable.pic5,
                R.drawable.pic6,
                R.drawable.pic7,
                R.drawable.pic8,
                R.drawable.correct,
                R.drawable.wrong,

                };

        Random soru = new Random();
        final int[] rastgele = new int[1];
        for (int i=0; i<1; i++)
                {rastgele[i]= soru.nextInt(8);}

         ArrayList<Integer> cevap = new ArrayList<Integer>();          
         for (int k = 0; k <= 7; ++k) 
            {cevap.add(k);}
         Collections.shuffle(cevap);

         final Integer[] rastgele2 = new Integer[4];
                    if (rastgele[0]!=cevap.get(0))
                    {rastgele2[0]=cevap.get(0);}
                    else
                    {rastgele2[0]=cevap.get(3);}
                    if (rastgele[0]!=cevap.get(1))
                    {rastgele2[1]=cevap.get(1);} 
                    else
                    {rastgele2[1]=cevap.get(3);}
                    if (rastgele[0]!=cevap.get(2))
                    {rastgele2[2]=cevap.get(2);} 
                    else
                    {rastgele2[2]=cevap.get(3);}                    
                    rastgele2[3]=rastgele[0];
                    Collections.shuffle(Arrays.asList(rastgele2));  

        view.setText(questions.get(rastgele[0]));
        answer1.setBackgroundResource(answers[rastgele2[0]]);
        answer1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            if (rastgele[0]==rastgele2[0])
                {answer1.setBackgroundResource(answers[8]);
                questions.remove(rastgele[0]);}
            else {answer1.setBackgroundResource(answers[9]);}
                                        }
        });
        answer2.setBackgroundResource(answers[rastgele2[1]]); 
        answer2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            if (rastgele2[1]==rastgele[0])
            {answer2.setBackgroundResource(answers[8]);
                countries.remove(rastgele[0]);}
            else {answer2.setBackgroundResource(answers[9]);}
                                        }
        });
        answer3.setBackgroundResource(answer[rastgele2[2]]);   
        answer3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (rastgele2[2]==rastgele[0])
            {answer3.setBackgroundResource(answer[8]);
                countries.remove(rastgele[0]);}
            else {answer3.setBackgroundResource(answer[9]);}
                                        }
        });
        answer4.setBackgroundResource(answer[rastgele2[3]]);
        answer4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (rastgele2[3]==rastgele[0])
            {answer4.setBackgroundResource(answer[8]);
                countries.remove(rastgele[0]);}
            else {answer4.setBackgroundResource(answer[9]);}
    }
        });}
} 
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 1
    `What can I do?` with what? `Can anyone help me for that?` with what? for FSM sake where is the question? what is not working? – Selvin Jul 05 '13 at 14:59

2 Answers2

0

to move to a new activity, see this post How to open a second activity on click of button in android app

In your existing layout you need this

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:onClick="RandomButtonNameHere"
    android:text="@string/button" />

Then in your existing JAVA you want to launch a new activity intent onclick

public void RandomButtonNameHere(View view) 
{
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}

where FromActivity is the name of your current java file and ToActivity is the name of your new java file, ignoring the .java extensions

I.e. Question1.Java to Question2.Java would be

public void ButtonToQuestion2(View view) 
{
Intent intent = new Intent(Question1.this, Question2.class);
startActivity(intent);
}

and called by the button in your Question1.xml

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:onClick="ButtonToQuestion2"
    android:text="@string/ButtonToQuestion2" />

Note that the name of my button is defined in my strings list but you could rename "@string/ButtonToQuestion2" to "Name of Button" for a static name

Community
  • 1
  • 1
Henry Aspden
  • 1,863
  • 3
  • 23
  • 45
  • But I want to generate random lots of questions. So I cant use new activity for all of those. All must be in aactivity. – Fırat Cihangir Kırbay Jul 05 '13 at 15:12
  • ahh right, sorry about that, I didn't realise / see that you wanted random questions... Could you load the questions from a database? if stored in your SQLite DB on the phone? This would also easily enable you to update the questions at a later date, without having to change the whole app... – Henry Aspden Jul 08 '13 at 09:37
  • Check out this post for details on SQLite Random Selecting... http://stackoverflow.com/questions/2279706/select-random-row-from-an-sqlite-table – Henry Aspden Jul 08 '13 at 09:40
0

Given that you use the same view to indicate correctness of the answer, you need some additional trigger - I'd suggest to add "next" button to your layout.

Then, factor out piece of code that sets views for a question (selecting country and resources for the answers) into a method void setUpQuestion(int questionIndex) that takes question number as an input. Create memver vaiable int currentQuestion = 0 for using as an input to setUpQuestion()

In OnClick() method for "next" button increment currentQuestion and call setUpQuestion() to update views.

amoiseyev
  • 327
  • 2
  • 7