steven
steven

Reputation: 698

How do I get this string array to pass over to the next class and be used

This is kind of hard to explain. Basically it's kind of like a simple game. I want the people to input their names (currently the submit button is not working correctly) hit submit for each name and when all names are in they hit play. It then opens up the next class. It needs to get the string array from the prior class as well as the number of players. It then needs to select each persons name in order and give them a task to do (which it randomly generates). Then it allows the other people to click a button scoring how they did. (I am not sure how to set up the score system. Not sure if there is a way to assign a score number to a particular array string) I would then like it after 5 rounds to display the winner. If you have any input or could help me out I would be extremely grateful. Thanks for taking the time... here are the two classes i have.

Class 1

public class Class1 extends Activity
{  
int players=0;
String names[];

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.class1);

    final EditText input = (EditText) findViewById(R.id.nameinput);


    Button submitButton = (Button) findViewById(R.id.submit_btn);
    submitButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View submit1)
        {
            players++;
            for(int i=0; i < players; i++)
            {
                names[i] = input.getText().toString();
                input.setText("");
            }
        }
    });

    Button doneButton = (Button) findViewById(R.id.done_btn);
    doneButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View done1)
        {
            Intent done = new Intent(Class1.this, Class2.class);
            done.putExtra("players", players);
            done.putExtra("names", names[players]);
            startActivity(done);
        }
    });
}

Class 2

public class Class2 extends Activity
{
int players, counter, score, ptasks,rindex;
String[] names, tasks;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.class2);

    Intent game = getIntent();
    players = game.getIntExtra("players", 1);
    names = game.getStringArrayExtra("names");

    Random generator = new Random();

    tasks[0]= "task1";
    tasks[1]= "task2";
    tasks[2]= "task3";
    tasks[3]= "task4";
    tasks[4]= "task5";
    tasks[5]= "task6";
    tasks[6]= "task7";
    tasks[7]= "task8";
    tasks[8]= "task9";
    tasks[9]= "task10";



    while (counter <5)
    {
        for (int i = 0; i < players; i++)
        {
            TextView name1 = (TextView) findViewById(R.id.pname);
            name1.setText( names[i]+":");

            ptasks = 10;
            rindex = generator.nextInt(ptasks);

            TextView task = (TextView) findViewById(R.id.task);
            task.setText( tasks[rindex]);


        }
        Button failButton = (Button) findViewById(R.id.fail_btn);
        failButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View failed)
            {
                //not sure what to put here to get the scores set up
            }
        });

        Button notButton = (Button) findViewById(R.id.notbad_btn);
        notButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View notbad)
            {
                //not sure what to put here to get the scores set up
            }
        });

        Button champButton = (Button) findViewById(R.id.champ_btn);
        champButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View champp)
            {
                //not sure what to put here
            }
        });


        counter++;
    }


}

I'm sure this thing is riddled with errors. And I'm sorry if it is I'm not that well experienced a programmer. Thanks again

Upvotes: 0

Views: 505

Answers (3)

kushaldsouza
kushaldsouza

Reputation: 708

You can pass a string array from one activity to another using a Bundle.

Bundle bundle = new Bundle();
bundle.putStringArray("arrayKey", stringArray);

You can then access this stringArray from the next activity as follows:

Bundle bundle = this.getIntent().getExtras();
String[] stringArray = bundle.getStringArray("arrayKey");

I'm not sure if this is the only thing you intend to do. I hope it helps. Also, to assign a score to a particular string array, assuming your scores are int's you could use a HashMap as follows,

HashMap<String[],int> imageData = new HashMap<String[],int>();

But I'm not sure how you would pass this Map to another activity if you intend to do so.

Upvotes: 2

anticafe
anticafe

Reputation: 6892

Use this cheat:

  • In Class2, convert you array string (tasks) to string (strSavedTask)by adding "|" separator. After that, pass your strSavedTask into Bundle and start to Class1.

  • When return to Class1, read strSavedTask from Bundle, split it by "|".

That's my cheat to pass array between 2 activity ^^

Hope this way can help you!

Upvotes: -1

Related Questions