Reputation: 99
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?
Upvotes: 0
Views: 91
Reputation: 31
To pass data from one Activity to Another, you can use:
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");
Upvotes: 0