简体   繁体   English

如何在活动之间传递Jsoup Elements对象?

[英]How do you pass a Jsoup Elements object between activities?

I have an activity which logs into my school gradebook, retrieves grades, and stores them in an Elements object. 我有一个活动可以登录到我的学校成绩簿中,检索成绩并将其存储在Elements对象中。 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. 但是,我现在需要将此对象传递给另一个活动,该活动会将成绩连接到recyclerview ,我不知道如何实现此目的。 I tried doing something like this, but I don't know how to extract the Elements in the second activity. 我尝试做这样的事情,但是我不知道如何在第二个活动中提取Elements 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 . 您可以使用Google的Gson将您的Elements对象序列化为Json字符串,然后通过Intent.putExtra(key, jsonString)将其放入您的意图中Intent.putExtra(key, jsonString)然后将其反序列化为目标ActivityElements对象。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM