I know it\'s a commonly asked question and I looked at the solutions online, but I am having a difficulty implementing this on my own.
I have a class which contains thr
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.
public class ParcelableListOfLists implements Parcelable {
private ArrayList<ArrayList<imageHolder>> listOfLists;
public ParcelableListOfLists(ArrayList<ArrayList<imageHolder>> listOfLists) {
this.listOfLists = listOfLists;
}
public ArrayList<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:
@Override
public void writeToParcel(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:
public ParcelableListOfLists(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 = new Intent(loadImages.this, storiesScreen.class);
nextActivity.putExtra("images", new ParcelableListOfLists(images));
startActivity(nextActivity);
and retrieve them in the next activity like this:
ParcelableListOfLists plol = getIntent().getParcelableExtra("images");
ArrayList<ArrayList<imageHolder>> images = plol.getListOfLists();
Ok.
Your Model
public class ImageHolder implements Serializable {
// Use String here, to avoid serialization problems
private String uri;
private Date date;
private Location loc;
public ImageHolder() {
}
public ImageHolder(String uri, Date date, Location loc) {
this.uri = uri;
this.date = date;
this.loc = loc;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Location getLoc() {
return loc;
}
public void setLoc(Location loc) {
this.loc = loc;
}
@Override
public String toString() {
return "ImageHolder{" +
"uri=" + uri +
", date=" + date +
", loc=" + loc +
'}';
}
}
Create a Container class
public class Container implements Serializable {
private ArrayList<ImageHolder> list;
public Container() {
}
public Container(ArrayList<ImageHolder> list) {
this.list = list;
}
public ArrayList<ImageHolder> getList() {
return list;
}
public void setList(ArrayList<ImageHolder> list) {
this.list = list;
}
@Override
public String toString() {
return "Container{" +
"list=" + list +
'}';
}
}
Your First Activity
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
ImageHolder imageHolder0 = new ImageHolder("http://www.stackoverflow.com", new Date(), new Location("test0"));
ImageHolder imageHolder1 = new ImageHolder("http://www.google.com", new Date(), new Location("test1"));
ArrayList<ImageHolder> list = new ArrayList<>();
list.add(imageHolder0);
list.add(imageHolder1);
Container container = new Container(list);
Gson gson = new Gson();
String json = gson.toJson(container);
Intent intent = new Intent(MainActivity.this, TestActivity.class);
intent.putExtra("yourKey", json);
startActivity(intent);
}
}
Your second activity
public class TestActivity extends AppCompatActivity {
public static final String TAG = TestActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Gson gson = new Gson();
Container container = gson.fromJson(getIntent().getStringExtra("yourKey"), Container.class);
Log.d(TAG, "OK?" + container.getList().toString());
// Here convert String uri Fields to Uri Objects
// Example:
Uri uri = 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!
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
Gson gson = new Gson();
String json = gson.toJson(yourObject);
intent.putExtra("yourKey", json);
In the second activity
Gson gson = new Gson();
YourObject yourObject = gson.fromJson(getIntent().getStringExtra("yourKey"), YourObject.class);
Easy and fast.