Passing ArrayList through Intent
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
public class Question implements Serializable {
...
...
...
}
in your current activity you probably have an ArrayList as follows
ArrayList<Question> qsList = new ArrayList<>();
qsList.add(new Question(1));
qsList.add(new Question(2));
qsList.add(new Question(3));
// intialize Bundle instance
Bundle b = new Bundle();
// putting questions list into the bundle .. as key value pair.
// so you can retrieve the arrayList with this key
b.putSerializable("questions", (Serializable) qsList);
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
i.putExtras(b);
startActivity(i);
in order to get the arraylist within the next activity
//get the bundle
Bundle b = getIntent().getExtras();
//getting the arraylist from the key
ArrayList<Question> q = (ArrayList<Question>) b.getSerializable("questions");
if you using Generic Array List with Class instead of specific type like
EX:
private ArrayList<Model> aListModel = new ArrayList<Model>();
Here, Model = Class
Receiving Intent Like :
aListModel = (ArrayList<Model>) getIntent().getSerializableExtra(KEY);
MUST REMEMBER:
Here Model-class must be implemented like: ModelClass implements Serializable
In your receiving intent you need to do:
Intent i = getIntent();
stock_list = i.getStringArrayListExtra("stock_list");
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:
stock_list = getIntent().getStringArrayListExtra("stock_list");
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:
public class Cars {
public String id, name;
}
FirstActivity.java
When you want to pass ArrayList:
List<Cars> cars= new ArrayList<Cars>();
cars.add(getCarModel("1", "A"));
cars.add(getCarModel("2", "B"));
cars.add(getCarModel("3", "C"));
cars.add(getCarModel("4", "D"));
Gson gson = new Gson();
String jsonCars = gson.toJson(cars);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("list_as_string", jsonCars);
startActivity(intent);
Get CarsModel by Function:
private Cars getCarModel(String id, String name){
Cars cars = new Cars();
cars.id = id;
cars.name = name;
return cars;
}
SecondActivity.java
You have to import java.lang.reflect.Type
;
on onCreate() to retrieve ArrayList:
String carListAsString = getIntent().getStringExtra("list_as_string");
Gson gson = new Gson();
Type type = new TypeToken<List<Cars>>(){}.getType();
List<Cars> carsList = gson.fromJson(carListAsString, type);
for (Cars cars : carsList){
Log.i("Car Data", cars.id+"-"+cars.name);
}
Hope this will save time, I saved it.
Done