Reputation: 47
I have ArrayList Of Custom Objects
ArrayList<AvailableCountry>
listAvailableCountries = new ArrayList();
and I have two objects of AvailableCountry
AvailableCountry obj1=new AvailableCountry();
AvailableCountry obj2=new AvailableCountry();
Then i have added to arraylist
listAvailableCountries.add(obj1);
listAvailableCountries.add(obj2);
And this is Class of Custom Object
public class AvailableCountry {
@SerializedName("Disabled")
@Expose
private Boolean disabled;
@SerializedName("Selected")
@Expose
private Boolean selected;
@SerializedName("Text")
@Expose
private String text;
@SerializedName("Value")
@Expose
private String value;
public Boolean getDisabled() {
return disabled;
}
public void setDisabled(Boolean disabled) {
this.disabled = disabled;
}
public Boolean getSelected() {
return selected;
}
public void setSelected(Boolean selected) {
this.selected = selected;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Now i want to pass this arraylist From Activity A to Activity B. and then i want to receive in Activity B. how can i achieve this task.
Upvotes: 1
Views: 70
Reputation: 47
using Gson we can easily share the data
compile 'com.google.code.gson:gson:2.4'
send like this in A.class
Intent intent = new Intent(A.this, B.class);
String favJSONString = new Gson().toJson(listAvailableCountries);
intent.putExtra("listAvailableCountries", favJSONString);
startActivity(intent);
and receive like this B.class
try {
Type type = new TypeToken<List<AvailableCountry>>() {
}.getType();
ArrayList<AvailableCountry> listAvailableCountries = new Gson().fromJson(getIntent().getStringExtra("listAvailableCountries"), type);
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 439
1) First of all implement Serializable interface to your Custom Object Class
public class AvailableCountry implements Serializable {
@SerializedName("Disabled")
@Expose
private Boolean disabled;
@SerializedName("Selected")
@Expose
private Boolean selected;
@SerializedName("Text")
@Expose
private String text;
@SerializedName("Value")
@Expose
private String value;
public Boolean getDisabled() {
return disabled;
}
public void setDisabled(Boolean disabled) {
this.disabled = disabled;
}
public Boolean getSelected() {
return selected;
}
public void setSelected(Boolean selected) {
this.selected = selected;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
2) Send the arrayList from your Class A to Class B
Intent intent =new Intent(A.this,B.class);
intent.putExtra("listCountry", (Serializable) listAvailableCountries);
startActivity(intent)
3) Receive the arraylist in Class B
ArrayList<AvailableCountry>listAvailableCountries = (ArrayList<AvailableCountry>) getIntent().getSerializableExtra("listCountry");
Upvotes: 1