0

I'm developing some kind of "score tracker" app for a certain game. A user adds a certain amount of players (the number is currently unlimited) and those player names are then added to ArrayList. Then on the next activity, the user must choose a playername from a Spinner and enter a certain amount of "points" or let's say "score" for that player.

This is my current code for this:

public void submitScore(View v){
    LinearLayout lLayout = (LinearLayout) findViewById (R.id.linearLayout);
    final int position = playerList.getSelectedItemPosition();
    EditText input = (EditText) findViewById(R.id.editText1);
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final TextView newTextView = new TextView(this);
    String enteredText = input.getText().toString();
    
    if (enteredText.matches(""))
    {
        emptyTextError();
    }
    else
    {
        //NEW TEXTVIEW
        newTextView.setLayoutParams(lparams);
        newTextView.setText(players.get(position) + " " + score);
        newTextView.setTextSize(20);
        lLayout.addView(newTextView);
    }
}

As you can see, a user enters a certain score and a new textview is created containing player name and current score.

Now what I want to do is implement a feature that will track score for each player.

Example: The user added 2 players, one named John and one named Jack. The user then added 20 points to John and then after a while another 20, also to john. Now the textViews should look like this:

John 20

John 40

And then if the user will add 10 points to Jack and another 20 to John, TextViews should look like this:

John 20

John 40

Jack 10

John 60

This is what I don't know what to do. How do I implement a new int variable for each ArrayList element? Or is there a better way to do this than making int variables?

I need the app to automatically generate ints accoring to ArrayList, if the ArrayList contains 5 players, 5 ints need to be created because I don't know how many players the user will enter.

2
  • 1
    Why not use a Map or a List of <Player,score> compositions? Commented Aug 28, 2013 at 13:48
  • @rocketboy I've never done anything like that, I'm still a beginner. Could you add some explanation on how to do this? Thank you Commented Aug 28, 2013 at 13:50

3 Answers 3

1

You should create a class, perhaps called 'Player'. Each player will have a String value name and an int value score. Then you can add these Player instances to the array each time a new Player is created. See how to do it in the Java class toturial

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I will give it a try and let you know how it goes
Also, OP should learn about Maps and other collections at the Collections tutorial
0
     class PlayerScore{

         String userName;
         int score;
         public PlayerScore(String userName, int score) {
                    this.userName = userName;
                    this.score = score;
         }

       //setter and getter

      }    

       List<PlayerScore> playerScore=new ArrayList<PlayerScore>();
       playerScore.add(new PlayerScore("John",20));
       playerScore.add(new PlayerScore("John",40));
       playerScore.add(new PlayerScore("Jack",10));
       playerScore.add(new PlayerScore("John",60)); 

1 Comment

this is not what Matthew meant. He adds 2x20 which is 40. You are adding 40 directly. So there needs to be some audit/history property for the scores in the player object.
0

Try using a Map<String,Integer>, for faster access try HashMap<~>

    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("John", 10);
    map.put("Bob", 40);

If you want to sort by name use a TreeMap. If you want to sort by score read this old thread: Sort a Map<Key, Value> by values (Java)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.