0

I am doing Kids Game, can say that it's IQ Kids Testing, and I have questions in 10 activity then I want it random when user launching the question it's will call 1 of 10 and when user press NEXT Question it will call 1 of 9 activity remaining., and if press Next Again it will call 1 of 8 activity remaining.

How can I do that?

Best Regards,

user1731690
  • 225
  • 1
  • 3
  • 16
  • 1
    seems like a bad design to me. The 1-9 question should be able to displayed in one activity. Means the activity should be more reusable and the question should be replaceable inside a placeholder of the activity. – Rudy May 17 '13 at 06:08
  • [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) Stack Exchange is here to help you be a better programmer, not write the code for you. – Ken Y-N May 17 '13 at 06:08
  • first find a random number between 1-10.this random number store in array .and next button fetch number from the array and start related activity .give layout name one,two,three... on Activity name OneActivity,TwoActivity ....then easily manage. – Hemantvc May 17 '13 at 06:10

4 Answers4

0
  1. Create a Hashtable / map with key as numbers from 1 to 10 and values as your activity names .

  2. When user presses next - Generate a random number between 1 and 10 .

  3. Use the random generated number as key and find the activity name . And if the random number is not found as key - generate random number again and repeat step 3 .

  4. Call start activity with the activity name .

Also every time you use a key/activity in start activity - Remove it from the hash table.

This can be one of the solutions though i am sure that people will come up with better solutions.

Community
  • 1
  • 1
Anukool
  • 5,301
  • 8
  • 29
  • 41
0
  1. Create an array, just put the value so

    array[0] = 1 // means array[0] is question 1

    array[1] = 2

    array[9] = 10

  2. Do a resshufle of the array ( think it like resshuffling card )

    pick 2 different index

    int index_a = Random.nextInt(10);

    int index_b = Random.nextInt(10);

  3. Exchange the value inside array[index_a] with array_index[b]
  4. Repeat 2 and 3 several times.
  5. you get a random sequence of array.
  6. create an activity display question based on the array.
  7. create a button, clicking it will replace the question from the same activity.

    Note : Do not create an activity for each of the question!

Rudy
  • 7,008
  • 12
  • 50
  • 85
0

Do you actually need 10 Activities? IMHO -> NO

You just keep one Activity and based on that obviosly you may have differnt screen design for different quizzes , then you may create various layouts based on a random selection of a particular number.

Now the question becomes , How do I get this random number.

How to generate random within a range

Let's say you have 10 different quizzes that may eventually increase, Hence you may use a good Data Structure or Collection. And for your use case I would recomment an Arraylist of HashMaps.

Arraylist count would tell how many tests are there and Hashmap would comprise of question number as key and value will contain the other data. It may not be optimized enough but its just a go for you.

Community
  • 1
  • 1
Prateek
  • 3,923
  • 6
  • 41
  • 79
0

I have created one demo for three activity , You can customise up 50 activity.

activity_main.xml

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="main" />

<Button
    android:id="@+id/btn_next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Next" />

<Button
    android:id="@+id/btn_random"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="29dp"
    android:layout_marginTop="70dp"
    android:text="Random" />

one.xml

 <TextView
     android:id="@+id/btn_random"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:layout_marginLeft="74dp"
     android:layout_marginTop="26dp"
     android:text="One" />

two.xml

<TextView

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:layout_marginLeft="74dp"
     android:layout_marginTop="26dp"
     android:text="Two" />
 <Button
     android:id="@+id/btn_random"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"
     android:text="Next" />

three.xml

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:layout_marginLeft="74dp"
     android:layout_marginTop="26dp"
     android:text="Three" />

 <Button 
     android:id="@+id/btn_random"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next" />

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
    public static List<Integer> numberList =new ArrayList<Integer>();
    private Button btn_Random,btn_Next;
    public static int countNext=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // set resources
        btn_Random =(Button) findViewById(R.id.btn_random);
        btn_Next =(Button) findViewById(R.id.btn_next);
        btn_Random.setOnClickListener(this);


        btn_Next.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_random:

            findRandom();

            break;
        case R.id.btn_next:
            countNext=0;

            System.out.println("first value of array:"+numberList.get(countNext));
            if(numberList.get(countNext)==1){
                countNext++;
                startActivity(new Intent(MainActivity.this,OneActivity.class));
            }else if (numberList.get(countNext) == 2) {
                countNext++;
                startActivity(new Intent(MainActivity.this,TwoActivity.class));
            }else if (numberList.get(countNext) ==3) {
                countNext++;
                startActivity(new Intent(MainActivity.this,ThreeActivity.class));           
            }




            break;
        default:
            break;
        }

    }

    private void findRandom() {
        numberList.clear();
        for(int i=0;i<3;i++){
            numberList.add(i+1);
        }

        long seed = System.nanoTime();
        Collections.shuffle(numberList, new Random(seed));
        Collections.shuffle(numberList, new Random(seed));


        for (int no : numberList) {
            System.out.println("random number:"+no);
        }

    }

}

OneActivity.java

package com.project.ques;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class OneActivity extends Activity implements OnClickListener{
    public static List<Integer> numberList =new ArrayList<Integer>();
    private Button btn_Random;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);
        // set resources
        btn_Random =(Button) findViewById(R.id.btn_random);

        btn_Random.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_random:

            System.out.println("first value of array:"+MainActivity.numberList.get(MainActivity.countNext));
            if(MainActivity.numberList.get(MainActivity.countNext)==1){
                MainActivity.countNext++;
                startActivity(new Intent(OneActivity.this,OneActivity.class));
            }else if (MainActivity.numberList.get(MainActivity.countNext) == 2) {
                MainActivity.countNext++;
                startActivity(new Intent(OneActivity.this,TwoActivity.class));
            }else if (MainActivity.numberList.get(MainActivity.countNext) ==3) {
                MainActivity.countNext++;
                startActivity(new Intent(OneActivity.this,ThreeActivity.class));            
            }



            break;

        default:
            break;
        }

    }



}

TwoActivity.java

package com.project.ques;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TwoActivity extends Activity implements OnClickListener{

    private Button btn_Random;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);
        // set resources
        btn_Random =(Button) findViewById(R.id.btn_random);

        btn_Random.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_random:


            System.out.println("first value of array:"+MainActivity.numberList.get(MainActivity.countNext));
            if(MainActivity.numberList.get(MainActivity.countNext)==1){
                MainActivity.countNext++;
                startActivity(new Intent(TwoActivity.this,OneActivity.class));
            }else if (MainActivity.numberList.get(MainActivity.countNext) == 2) {
                MainActivity.countNext++;
                startActivity(new Intent(TwoActivity.this,TwoActivity.class));
            }else if (MainActivity.numberList.get(MainActivity.countNext) ==3) {
                MainActivity.countNext++;
                startActivity(new Intent(TwoActivity.this,ThreeActivity.class));            
            }



            break;

        default:
            break;
        }

    }



}

ThreeActivity.java

package com.project.ques;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ThreeActivity extends Activity implements OnClickListener{

    private Button btn_Random;
    public static int countNext=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.three);
        // set resources
        btn_Random =(Button) findViewById(R.id.btn_random);

        btn_Random.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_random:


            System.out.println("first value of array:"+MainActivity.numberList.get(countNext));
            if(MainActivity.numberList.get(countNext)==1){
                MainActivity.countNext++;
                startActivity(new Intent(ThreeActivity.this,OneActivity.class));
            }else if (MainActivity.numberList.get(countNext) == 2) {
                MainActivity.countNext++;
                startActivity(new Intent(ThreeActivity.this,TwoActivity.class));
            }else if (MainActivity.numberList.get(countNext) ==3) {
                MainActivity.countNext++;
                startActivity(new Intent(ThreeActivity.this,ThreeActivity.class));          
            }

            countNext++;
            break;

        default:
            break;
        }

    }



}

AndroidManifest.xml

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.project.ques.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="OneActivity"></activity>
    <activity android:name="TwoActivity"></activity>
    <activity android:name="ThreeActivity"></activity>
</application>

Hemantvc
  • 2,111
  • 3
  • 30
  • 42