-2

I have a listView (for player names entered in the edittext on my loginpage). My second activity page (the actual game refers to those player names (projects the first of the list top of the screen as can be seen here:

enter image description here

In order to do so, I created the following intent on the loginpage:

public void openActivity2() {
    Intent intent = new Intent(this, Gamescreen.class);
    String s= list.get(0);
    intent.putExtra("Name1", s);
    startActivity(intent);
}}

In the game screen I refer to this as such:

Intent iin= getIntent();
Bundle b = iin.getExtras();

if(b!=null)
{
    String j =b.getString("Name1");
    playerturn.setText(j);
}

Basically what I want is that when I press the button (next player) on the gamescreen that it moves from the first player name on the list to the second. As such as you keep clicking the next player button -> you move down the list.

I will need to make an onclicklistener for the click of the button, but how do I make the rest possible, with the current way I have approached it? Help much appreciated!

Shruti
  • 803
  • 9
  • 26

1 Answers1

0

What I will do is to store the name string list to the local storage like the SharedPreferences in the loginpage:

    Gson gson = new Gson();
    String playerNameListString = gson.toJson(list);
    SharedPreferences.Editor editor = getSharedPreferences("prefs_file_name", 0).edit();
    editor.putString("player_name_list", playerNameListString);
    editor.commit();

And in the gamescreen, restore the name string with:

    String playerNameListString = getSharedPreferences("prefs_file_name", 0).getString("player_name_list", null);
    Gson gson = new Gson();
    final String[] playerNameArray = gson.fromJson(playerNameListString, String[].class);
    nextplayer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            playerIndex++;
            if (playerIndex > playerNameArray.length) {
                playerIndex = 0;
            }
            playerturn.setText(playerNameArray[playerIndex]);
        }
    });
Pai-Hsiang Huang
  • 342
  • 3
  • 12
  • https://gyazo.com/fc4ab30eaa4028b6e006551eb9c3a88a. @Pai-Hsiang Huang . Do I place it within my intent? And where would I place the 2nd part in within the gamescreen? – Boris Cornelis Apr 10 '18 at 22:25
  • @BorisCornelis Edited my answer which should remove the "red line" editor. You don't need to place within intent. And for the second part, you can place it anywhere in your gamescreen Activity as long as you need the player name list. For more information about the use of SharedPreferences, you could check the [official document](https://developer.android.com/reference/android/content/SharedPreferences.html). – Pai-Hsiang Huang Apr 11 '18 at 01:29
  • https://gyazo.com/3d4c4bcd23ed18a360d1460e7d55065f - @Pai-Hsiang Huang. what do I then write here? If i want it to grab the first item from the list and every time you click it moves down that list? – Boris Cornelis Apr 11 '18 at 08:52
  • @BorisCornelis Edit the answer with putString() and getString() instead of using string set. Also, use Gson to serialize and de-serialize the string array. The idea of using gson comes from [this post](https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) – Pai-Hsiang Huang Apr 11 '18 at 14:44