0

I have several List<String> variables to pass from Splash to the Main activity:

1) I read somewhere that I can pass them as ArrayList<String> from Splash to Main, and it works... i.e. I can receive only the first ArrayList<String> variable. In my bundle below, I am not able to receive the second ArrayList<String>. (array_list2) Why?

2) How to pass ArrayList<LatLng> from one activity to another

First Activity:

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);     
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
b.putStringArrayList("array_list5",(ArrayList<LatLng>)coordinates); //ERROR in this line, type mismatch!
intent.putExtras(b);
startActivity(intent);

Second Activity:

Bundle b = getIntent().getExtras();
if (b != null) {
testList1 = b.getStringArrayList("array_list1");
testList2 = b.getStringArrayList("array_list2"); //THIS gives the same arraylist as testList1 and it is incorrect!
Log.e("TESTLIST1",testList1.toString()); //just using Log.e to view o/p as test
Log.e("TESTLIST2",testList1.toString());

Please answer both my questions. None of the other topics helped me, and I spent over 2 hours on this. Thank you.

2
  • Why casting every object to ArrayList<String> ? Commented Dec 3, 2015 at 6:28
  • Only ArrayList<String> is supported with method putStringArrayList and there is no other method for List<String> Commented Dec 3, 2015 at 6:32

2 Answers 2

2

putStringArrayList() will not support for ArrayList .

use

 putParcelableArrayList();

instead of

 putStingArrayList();

or else you can use direct method of intent.

Change your code somethng like this:

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);     
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);

If you want to pass through bundle object use below line of code.

b.putParcelableArrayList("array_list5",(ArrayList<LatLng>)coordinates);

Otherwise pass through intent object.

intent.putParcelableArrayListExtra("array_list5",coordinates);
intent.putExtras(b);
startActivity(intent);
Sign up to request clarification or add additional context in comments.

9 Comments

Can you please explain for first, and second activities for both List<String> and ArrayList<LatLng>? Is "intent.putExtras(b);" correct?
intent,putExtras(b); is correct only..LatLng class is a parcelable class so should use putParcelableArrayList("array_list5",coordinates);
I get the error: "The method putParcelableArrayList(String, ArrayList<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, List<String>)" - also, pl. check the comments in my second activity... I am NOT receiving the second arraylist on the 2nd activity.
you should not type cast the arraylist. Anyway you are passing arrayList object only na..so type casting is not required.
I understand using putParcelableArrayList for LatLng. What is the syntax to use it for List<String> ? I have this error with your code - ""The method putParcelableArrayList(String, ArrayList<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, List<String>)""
|
1

Because LatLng class implements Parcelable interface, so instead of using Bundle. putStringArrayList use Bundle.putParcelableArrayList to send ArrayList which contains class object which is implementing Parcelable interface. Use

b.putParcelableArrayList("array_list5",coordinates);

How to use parcelable with List or ArrayList - What is the syntax?

Do it as:

ArrayList<String> arrCoordinates = new ArrayList<>(coordinates.size());
arrCoordinates.addAll(coordinates);
b.putParcelableArrayList("array_list5",arrCoordinates);

and get array_list5 as from Bundle

ArrayList<LatLng> coordinates = b.getParcelableArrayList("array_list5"); 

10 Comments

Can you please explain for first, and second activities for both List<String> and ArrayList<LatLng>? Is "intent.putExtras(b);" correct?
@Zac1: my question is Why casting every object to ArrayList<String> ?
Only ArrayList<String> is supported with method putStringArrayList and there is no other method for List<String>
Yes, the second arraylist is not received by the second activity. Why? Please look at the comments in my code. ALSO, using parcelable on List<String> gives error, "The method putParcelableArrayList(String, ArrayList<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, List<String>)"
@Zac1: ok then use (ArrayList<LatLng>)coordinates
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.