I am trying to pass an arrayList to another activity using intents. Here is the code in the first activity.
case R.id.editButton:
Toast.makeText(this, "edit was clicked", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, editList.class);
intent.putStringArrayListExtra("stock_list", stock_list);
startActivity(intent);
break;
This is where I try to retrieve the list in the second activity. Is something wrong here?
Intent i = new Intent(); //This should be getIntent();
stock_list = new ArrayList<String>();
stock_list = i.getStringArrayListExtra("stock_list");
In your receiving intent you need to do:
The way you have it you've just created a new empty intent without any extras.
If you only have a single extra you can condense this down to:
Suppose you need to pass an arraylist of following class from current activity to next activity // class of the objects those in the arraylist // remember to implement the class from Serializable interface // Serializable means it converts the object into stream of bytes and helps to transfer that object
in your current activity you probably have an ArrayList as follows
in order to get the arraylist within the next activity
if you using Generic Array List with Class instead of specific type like
EX:
Here, Model = Class
Receiving Intent Like :
MUST REMEMBER:
Here Model-class must be implemented like: ModelClass implements Serializable
I have done this one by Passing ArrayList in form of String.
Add
compile 'com.google.code.gson:gson:2.2.4'
in dependencies block build.gradle.Click on Sync Project with Gradle Files
Cars.java:
FirstActivity.java
When you want to pass ArrayList:
Get CarsModel by Function:
SecondActivity.java
You have to import
java.lang.reflect.Type
;on onCreate() to retrieve ArrayList:
Hope this will save time, I saved it.
Done
this my item . implement Serializable and create ArrayList
and put in Bundle
and create a new Intent that put Bundle to Intent
for receive bundle insert this code