如何传递List到 Xamarin.Android 中的另一个活动

发布于 2025-01-15 22:56:18 字数 481 浏览 1 评论 0原文

我有列表,我想将其传递给另一个活动 当我在第一个活动中使用 JsonConvert.serialize 对其进行序列化并在另一个活动中对其进行反序列化时,它仅在我将其记录到控制台时才有效,但是当我将其设置为值时,它会给出一个错误,指出该值为 null 。

//活动一 string jsonData = JsonConvert.SerializeObject(VideosList);意图意图 = new Intent(this, typeof(HomeActivity)); Intent.PutExtra("媒体", jsonData); StartActivity(intent);

//活动二 string jsonData2 = Intent.GetStringExtra("media"); // 我可以记录它 List

I have List and I want to pass it to another activity
when I Serialize it using JsonConvert.serialize in the first activity and deserialize it on the other one, It works only when I am logging it to console, but when I set it as a value it gives me an error that says the value is null.

//activity one
string jsonData = JsonConvert.SerializeObject(VideosList); Intent intent = new Intent(this, typeof(HomeActivity)); intent.PutExtra("media", jsonData); StartActivity(intent);

//activity two
string jsonData2 = Intent.GetStringExtra("media"); // i can log it List<Video> list = JsonConvert.DeserializeObject<List<Video>>(jsonData2); here give me null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满地尘埃落定 2025-01-22 22:56:18

首先,我认为数据类型不应该声明为字符串。所以你可以尝试使用下面的代码。

intent.PutExtra("media", JsonConvert.SerializeObject(VideosList)); 
StartActivity(intent);

并在其他活动中使用它:

var list = JsonConvert.DeserializeObject<List<Video>>(Intent.GetStringExtra("media"));

如果它不起作用,您可能需要自己实现 Parcelable 接口。

At first, I think the data type shouldn't be declared as the string. So you can try to use the following code.

intent.PutExtra("media", JsonConvert.SerializeObject(VideosList)); 
StartActivity(intent);

And use it in the other activity:

var list = JsonConvert.DeserializeObject<List<Video>>(Intent.GetStringExtra("media"));

If it doesn't work, you may need to implement the Parcelable interface by your self.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文