开发者

Array List transfering correctly & Setting a score to each array list member

To all you that have helped me with my other questions thank you. I almost have it, but 2 fi开发者_如何学运维nal problems are preventing it from working the way i want.

These 2 classes are supposed to do as follows. 1st class gets the names of the people that want to play the game. Uses the same EditText and when they input their name they click submit. When all the names are submitted they click the done/play button which sends them and their data (how many players and names) to the next class. On class 1 i believe the error lies in the submit button. I'm trying to add all the names to an array list and I dont believe it is doing it correctly. When I run the app it takes in the names just fine from the users standpoint. But on the following screen it should display their name: (it says null so it is not getting the names correctly) and a task to do (which it does correctly).

The last thing it needs to do is on class 2 it needs to allow those buttons (failed, champ, and not bad) to only need to be clicked once (then it sets a score to the name of the person who's turn it was) and then it needs to start the next person and task. (It does neither atm). I would really appreciate help getting this blasted thing to work. Thanks to all who take the time to reply. And sorry if ur sick of seeing my help requests.

Class 1

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

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

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

        final ArrayList<String> names = new ArrayList<String>();
        //names = new String[players];

        Button submitButton = (Button) findViewById(R.id.submit_btn);
        submitButton.setOnClickListener(new View.OnClickListener()
        {
             public void onClick(View submit1)
             {
                //for( i=i; i < players; i++)
                //{
                    players++;
                    names.add(input.getText().toString());
                    //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, Game.class);
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("arrayKey", names);

                done.putExtra("players", players);
                //done.putExtra("names", names[players]);
                startActivity(done);
            }
        });
    }

Game Class

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

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

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

        Random generator = new Random();

        tasks = new String[10];
        tasks[0]= "";
        tasks[1]= "";
        tasks[2]= "";
        tasks[3]= "";
        tasks[4]= "";
        tasks[5]= "";
        tasks[6]= "";
        tasks[7]= "";
        tasks[8]= "";
        tasks[9]= "";

        names = new String[players];

        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)
                    {
                        return;
                    }
                });

                Button notButton = (Button) findViewById(R.id.notbad_btn);
                notButton.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View notbad)
                    {
                        return;
                    }
                });

                Button champButton = (Button) findViewById(R.id.champ_btn);
                champButton.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View champp)
                    {
                        return;
                    }
                });

            }

            counter++;
        }
    }
}

As a side note. The things that you see within those sections that have // comments next to them I have there because i was testing out between those and the ones that arent commented out and neither worked. If you have any input on fixing any of this i appreciate it.


I see two problems with your code that might explain why you get a null for your players list in your second Activity:

  1. In Game, String[] names = bundle.getStringArray("arrayKey"); should be

    ArrayList<String> names = bundle.getStringArrayList("arrayKey");`
    
  2. In Class1, you're putting the ArrayList into the Bundle(bundle.putStringArrayList("arrayKey", names);) which is pointless since bundle goes no where. You should be putting it into the Intent instead:

    done.putStringListExtra("arrayKey", names);
    

Note that your code is all the more confusing because you have both a String [] named names and an ArrayList named names in different scopes. Decide on one (I'd recommend the List) and get rid of the other.

Also, in Game, this is unncessary:

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

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

You already have the bundle just before this, so you could as well do:

Bundle bundle = this.getIntent().getExtras();
String[] names = bundle.getStringArray("arrayKey");
players = bundle.getInt("players", 1);

The basic concept is that from the calling activity, you put information into an Intent using the various putExtra() and putExtraXXX() methods. In the called activity, you get the information you had put into the Intent by either

  • getting a Bundle *from * the Intent via getExtras() and then getting everything put in using the various get() methods on the Bundle (not the Intent).
  • directly invoking the getExtraXXX() methods on the Intent.

For the second part, as your code currently stands, it simply going to loop over all the players immediately (5 times in all, I don't understand the purpose of counter).

What you should instead be doing is performing all of your processing (calculating the score for the current player, incrementing the value of the player index, setting the next task etc) only when one of the 3 buttons is pressed. If it's going to be a long-lived task, you could disable the buttons until finished in order to enforce the requirement of allowing only one button to be pressed per player. Re-enable the buttons when the next player is ready.

I don't have the energy to churn out everything you need but at a starting point, turn this:

public void onCreate(Bundle savedInstanceState)
{
    //...other code here
    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)
                {
                    return;
                }
            });

            Button notButton = (Button) findViewById(R.id.notbad_btn);
            notButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View notbad)
                {
                    return;
                }
            });

            Button champButton = (Button) findViewById(R.id.champ_btn);
            champButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View champp)
                {
                    return;
                }
            });

        }

        counter++;
    }
    //...other code here
}

into

public void onCreate(Bundle savedInstanceState)
{
    //...other code here
    int i = 0;
    TextView name1 = (TextView) findViewById(R.id.pname);
    TextView task = (TextView) findViewById(R.id.task);        

    Button failButton = (Button) findViewById(R.id.fail_btn);
    failButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View failed)
        {
            //do what must be done for the current player, calculate score, etc
            prepareNextPlayer(++i, names, name1, task);
        }
    });

    Button notButton = (Button) findViewById(R.id.notbad_btn);
    notButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View notbad)
        {
            //do what must be done for the current player, calculate score, etc
            prepareNextPlayer(++i, names, name1, task);
        }
    });

    Button champButton = (Button) findViewById(R.id.champ_btn);
    champButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View champp)
        {
            //do what must be done for the current player, calculate score, etc
            prepareNextPlayer(++i, names, name1, task);
        }
    });
    //...other code here
}

private void prepareNextPlayer(int i, ArrayList<String> names, String [] tasks, TextView nameField, TextView taskField)
{
        if(i >= names.size())
        {
            //all players have been processed, what happens now?
            return;
        }

        int rindex = generator.nextInt(10);
        nameField.setText( names.get(i)+":");
        task.setText( tasks[rindex]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜