0

I need my arraylist (mNamelist) from ActivityPlayers.java and use that arraylist in my ActivityNewGame.java file. Also, it should update at the same time. For example in ActivityPlayers, the user gives a new name or delete an existing name, it should also insert or delete that name to the same list in ActivityNewGame.

This is my ActivityPlayers.java method. This method will be called when the user adds a new name to mNamelist.

private void addItem(int position) {
    /** Get user input (name) **/
    textAdd = findViewById(R.id.name_input);

    /** Add name to the list **/
    mNameList.add(position, new NameItem(textAdd.getText().toString().trim()));

    /** sort that list **/
    sortArrayList();

    /** save changes to shared preferences **/
    saveData();

    /** Show changed list to user **/
    mAdapter.notifyItemInserted(position);

    /** Clear the input field **/
    textAdd.getText().clear();

    /** Send namelist to ActivitynewGame **/
    Intent i = new Intent(ActivityPlayers.this, ActivityNewGame.class);
    i.putExtra("PlayerList", mNameList);
    startActivity(i);
}

This is my ActivityNewGame:

    public class ActivityNewGame extends AppCompatActivity {
        private ArrayList<NewGamePlayerItem> mPlayerList;

        private RecyclerView mRecyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager mLayoutManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_new_game);

            mPlayerList = new ArrayList<>();
            Intent intent = getIntent();
            ArrayList<NameItem> mNameList = (ArrayList<NameItem>) intent.getSerializableExtra("PlayerList");

            /** Check if mNameList has any items in it. **/
            if (mNameList.size() != 0) {
            /** Loop through that arraylist and set its names into this Activity items. ("true" is for checkbox that this item has.) **/
                for (int i = 0; i < mNameList.size(); i++) {
                    mPlayerList.add(new NewGamePlayerItem(true, mNameList.get(i).getText1()));
                }
            }

            buildRecyclerView();
        }

The problem here is that I don't want to visit ActivityNewGame, I just want to pass that name list from ActivityPlayers, and when user choose to go to make a new game (ActivityNewGame), then that player list would be there. So what should I do differently to manage to do that?

Antti
  • 89
  • 1
  • 5

2 Answers2

3

You have several options:

  1. Save the list in a SqlLite database and load/reload in activities.
  2. Pass the list in an Intent object, see this question
  3. Serialize in a shared preferences
onof
  • 17,167
  • 7
  • 49
  • 85
  • Can I do intent somehow, that I don't have to "visit" in ActivityNewGame, cause player might want to give more than 1 player to the list before go to the game, so thats why it should stay in ActivityPlayers – Antti Jul 10 '19 at 07:28
0

To pass data from one Activity to Another, you can use:

  1. SQlite Database
  2. Passing Object through Intent
  3. Using the Shared Preference

I feel Passing Object through Intent will best suit your requirement. To pass the Object through Intent the Object should be Serialized. i.e., It must implement the Serializable Interface.

In your example you are required to pass the ArrayList of NameItem. ArrayList itself is Serializable, so you now need to implement the Serializable Inferace in NameItem Class as below:

class NameItem implements Serializable{
    String name;
    //getters and setters
}

Now you can pass the ArrayList using Intent from ActivityPlayers to ActivityNewGame as below:

Intent i = new Intent(ActivityPlayers.this,ActivityNewGame.class);
i.putExtra("PlayerList",mNameList);
startActivity(i);

You can fetch the Data in ActivityNewGame out of Intent as below:

Intent intent = getIntent();
ArrayList<NameItem> mNameList = (ArrayList<NameItem>) intent.getSerializableExtra("PlayerList");
  • Can I do intent somehow, that I don't have to "visit" in ActivityNewGame, cause player might want to give more than 1 name to the list before going to the game, so that's why it should stay in ActivityPlayers? – Antti Jul 10 '19 at 10:37
  • If you want to stay in ActivityPlayers, then why you want to pass the data to ActivityNewGame? Just keep the Data in AcitivityPlayers. When you want to go to ActivityNewGame, then only pass the Data to that Activity. – Rupesh Chaudhari Jul 10 '19 at 11:48
  • Well I thought I have to do so... " When you want to go to ActivityNewGame, then only pass the Data to that Activity." How I do so? – Antti Jul 10 '19 at 11:56
  • Just add your ArrayList Object to Intent before calling the startActivity(intent) using intent.putExtra(obj); As shown in above answer. – Rupesh Chaudhari Jul 10 '19 at 14:22
  • Exactly that is what I am saying. Just pass the value to intent when the player wants to go on ActivityNewGame. Before visiting to ActivityNewGame, why would you want to pass the List to ActivityNewGame? Even if you are willing to have the list available everywhere, you can use the SharedPreference. Please check this link: https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Rupesh Chaudhari Jul 11 '19 at 05:45
  • I want to pass it because, ActivityPlayers is the place where user can give all players, and when the user goes to ActivityNewGame (starting a new game), he can choose which players from the list wants to play this time. User may not always want to play with all players on a list. – Antti Jul 11 '19 at 06:41
  • Yes, so pass the list through intent only when you are going to call the ActivityNewGame. This way when you call the ActivityList you will have the ArrayList available there. If you still have doubt share your Activity Code. – Rupesh Chaudhari Jul 11 '19 at 06:55
  • I updated my post the way u suggested. What I'm doing wrong, it still goes to that ActivityNewGame? – Antti Jul 11 '19 at 07:17
  • I feel that you are trting to build a realtime application. Where one player maybe on ActivityPlayers whereas the other player maybe on ActivityNewGame. You want the other player to view the PlayersList. Is it correct? – Rupesh Chaudhari Jul 11 '19 at 12:16
  • No, I'm building an application to keep disc golf scores, it's not the actual game itself, you just create "game" where the user first chooses players who join that round, then similar way, user choose a course, where that group of people play their round. My MainActivity has 4 buttons, new game, resume game, courses and players. courses and players are activities where user add or remove more players and courses but when user goes to new game, there is same list of people that he added in players but now in front of names has checkboxes, to choose all players and radio button for course. – Antti Jul 11 '19 at 13:17
  • Ok. That is nice to know. You can use SharedPreference for the same. Store you Arraylist in SharedPreference. Here is the link: https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Rupesh Chaudhari Jul 11 '19 at 14:27