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?