Passing An Arraylist Of Arraylists
Solution 1:
I think the best thing to do here would be to create a new class that implements Parcelable
and pass instances of this new class between activities.
publicclassParcelableListOfListsimplementsParcelable {
privateArrayList<ArrayList<imageHolder>> listOfLists;
publicParcelableListOfLists(ArrayList<ArrayList<imageHolder>> listOfLists) {
this.listOfLists = listOfLists;
}
publicArrayList<ArrayList<imageHolder>> getListOfLists() {
return listOfLists;
}
// parcelable implementation here
}
Once you have this class, you can be in full control of how your data is parceled, and that lets you do some things that you won't be able to do with the Android built-in offerings.
Here's one way you could parcel a list of lists:
@OverridepublicvoidwriteToParcel(Parcel dest, int flags) {
if (listOfLists != null) {
dest.writeInt(listOfLists.size());
for (ArrayList<imageHolder> list : listOfLists) {
dest.writeTypedList(list);
}
} else {
dest.writeInt(-1);
}
}
And on the other side, you can recreate the list of lists like this:
publicParcelableListOfLists(Parcel in) {
int size = in.readInt();
if (size != -1) {
this.listOfLists = new ArrayList<>(size);
for (int i = 0; i < size; ++i) {
ArrayList<imageHolder> list = in.createTypedArrayList(imageHolder.CREATOR);
listOfLists.add(list);
}
} else {
this.listOfLists = null;
}
}
With all of this together, you can pass your list of lists between activities like this:
Intent nextActivity = newIntent(loadImages.this, storiesScreen.class);
nextActivity.putExtra("images", newParcelableListOfLists(images));
startActivity(nextActivity);
and retrieve them in the next activity like this:
ParcelableListOfLists plol = getIntent().getParcelableExtra("images");
ArrayList<ArrayList<imageHolder>> images = plol.getListOfLists();
Solution 2:
I think that you can use Gson too.
In your build.gradle
implementation 'com.google.code.gson:gson:2.8.4'
In the first activity
Gsongson=newGson();
Stringjson= gson.toJson(yourObject);
intent.putExtra("yourKey", json);
In the second activity
Gsongson=newGson();
YourObjectyourObject= gson.fromJson(getIntent().getStringExtra("yourKey"), YourObject.class);
Easy and fast.
Solution 3:
Ok.
Your Model
publicclassImageHolderimplementsSerializable {
// Use String here, to avoid serialization problemsprivateString uri;
privateDate date;
privateLocation loc;
publicImageHolder() {
}
publicImageHolder(String uri, Date date, Location loc) {
this.uri = uri;
this.date = date;
this.loc = loc;
}
publicStringgetUri() {
return uri;
}
publicvoidsetUri(String uri) {
this.uri = uri;
}
publicDategetDate() {
return date;
}
publicvoidsetDate(Date date) {
this.date = date;
}
publicLocationgetLoc() {
return loc;
}
publicvoidsetLoc(Location loc) {
this.loc = loc;
}
@OverridepublicStringtoString() {
return"ImageHolder{" +
"uri=" + uri +
", date=" + date +
", loc=" + loc +
'}';
}
}
Create a Container class
publicclassContainerimplementsSerializable {
privateArrayList<ImageHolder> list;
publicContainer() {
}
publicContainer(ArrayList<ImageHolder> list) {
this.list = list;
}
publicArrayList<ImageHolder> getList() {
return list;
}
publicvoidsetList(ArrayList<ImageHolder> list) {
this.list = list;
}
@OverridepublicStringtoString() {
return"Container{" +
"list=" + list +
'}';
}
}
Your First Activity
publicclassMainActivityextendsAppCompatActivity {
publicstaticfinalStringTAG= MainActivity.class.getSimpleName();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
@OverrideprotectedvoidonPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
ImageHolderimageHolder0=newImageHolder("http://www.stackoverflow.com", newDate(), newLocation("test0"));
ImageHolderimageHolder1=newImageHolder("http://www.google.com", newDate(), newLocation("test1"));
ArrayList<ImageHolder> list = newArrayList<>();
list.add(imageHolder0);
list.add(imageHolder1);
Containercontainer=newContainer(list);
Gsongson=newGson();
Stringjson= gson.toJson(container);
Intentintent=newIntent(MainActivity.this, TestActivity.class);
intent.putExtra("yourKey", json);
startActivity(intent);
}
}
Your second activity
publicclassTestActivityextendsAppCompatActivity {
publicstaticfinalStringTAG= TestActivity.class.getSimpleName();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@OverrideprotectedvoidonPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Gsongson=newGson();
Containercontainer= gson.fromJson(getIntent().getStringExtra("yourKey"), Container.class);
Log.d(TAG, "OK?" + container.getList().toString());
// Here convert String uri Fields to Uri Objects// Example:Uriuri= Uri.parse(container.getList().get(0).getUri());
}
}
Logcat result: 08-01 11:10:22.097 6671-6671/it.darksurfer.english.template D/TestActivity: OK?[ImageHolder{uri=http://www.stackoverflow.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test0 0,000000,0,000000 acc=??? t=?!? et=?!?]}, ImageHolder{uri=http://www.google.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test1 0,000000,0,000000 acc=??? t=?!? et=?!?]}]
Obviously you can put an undefined number of others ArrayList in the list of Container class!
Post a Comment for "Passing An Arraylist Of Arraylists"