使用 Intent.putExtra 发送数组 [英] Sending arrays with Intent.putExtra

查看:45
本文介绍了使用 Intent.putExtra 发送数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在活动 A 中有一个整数数组:

I have an array of integers in the activity A:

int array[] = {1,2,3};

我想将该变量发送到活动 B,因此我创建了一个新意图并使用 putExtra 方法:

And I want to send that variable to the activity B, so I create a new intent and use the putExtra method:

Intent i = new Intent(A.this, B.class);
i.putExtra("numbers", array);
startActivity(i);

在活动 B 我得到信息:

In the activity B I get the info:

Bundle extras = getIntent().getExtras();
int arrayB = extras.getInt("numbers");

但这并不是真正发送数组,我只是在 arrayB 上得到了值0".我一直在寻找一些例子,但我没有找到任何东西.

But this is not really sending the array, I just get the value '0' on the arrayB. I've been looking for some examples but I didn't found anything so.

推荐答案

您正在使用数组设置额外内容.然后,您正在尝试获取单个整数.

You are setting the extra with an array. You are then trying to get a single int.

您的代码应该是:

int[] arrayB = extras.getIntArray("numbers");

这篇关于使用 Intent.putExtra 发送数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆