I have an activity which logs into my school gradebook, retrieves grades, and stores them in an Elements object. However, I now need to pass this object to another activity which will wire the grades into a recyclerview
, and I don't know how to accomplish this. I tried doing something like this, but I don't know how to extract the Elements
in the second activity. I am aware that there is another similar question but there was no clear solution for it.
Intent GradeList = new Intent(getApplicationContext(),GradeList.class);
GradeList.putExtra("grades",w.gradeList);
startActivity(GradeList);
Please help!
You can use Google's Gson to serialize your Elements
object into a Json string, put it into your intent via Intent.putExtra(key, jsonString)
then deserialize it back to an Elements
object in the target Activity
.
The code would be something like:
// I suppose w.GradeList returns an Elements object.
Gson gson = new Gson();
String json = gson.toJson(w.GradeList, Elements.class);
Intent intent = new Intent(getApplicationContext(), GradeList.class);
intent.putExtra(SOME_PUBLIC_STATIC_KEY, json);
// On the target Activity.
Intent intent = getIntent();
String json = intent.getStringExtra(SOME_PUBLIC_STATIC_KEY);
Gson gson = new Gson();
Elements elements = gson.fromJson(json, Elements.class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.