I am new in Android and I have a question regarding intent. I want to pass an String ArrayList between two activities by using intent. I have tried this:
`
ArrayList<String> playerName = new ArrayList<>();
Intent intent = new Intent(this,Game.class);
intent.putStringArrayListExtra("Player",playerName);
startActivity(intent);`
Then I tried to receive my intent like that: `
ArrayList<String> playerNameList= new ArrayList<>();
playerNameList = getIntent().getStringArrayListExtra("Player");
int listSize = playerNameList.size();
StringBuilder str = new StringBuilder(" ");
str.append(listSize);
textView.setText(str);
StringBuilder str1 = new StringBuilder(" ");
str1.append(playerNameList.get(2));
textView2.setText(str1);`
I can get the correct listSize; however I am not able to get any element of my arrayList. I always get the "index(0)" element. I would be really appreciated to any advice and help
you can easily to by using Gson
at Game.class
Check your code to add data to ArrayList
playerName
. The code toputStringArrayListExtra
to Intent andgetStringArrayListExtra
from Intent is correct.