本文介绍了Android,我可以使用 putExtra 来传递多个值吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个值传递给另一个活动,我可以使用 putExtra 来做到这一点,还是必须以更复杂的方式来做到这一点,从我的阅读中可以看出.例如,这样的事情可以工作吗?

I want to pass two values to another activity can I do this with putExtra or do I have to do it a more complicated way, which it seems from my reading. E.g.. can something like this work?

public final static String ID_EXTRA="com.fnesse.beachguide._ID";

Intent i = new Intent(this, CoastList.class);
i.putExtra(ID_EXTRA, "1", "111");
startActivity(i);

上面给出了一个错误.

编辑

我尝试的第一件事类似于:

The first thing I tried was similar to:

i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");

但 ID_EXTRA2 似乎覆盖了 ID_EXTRA1

but ID_EXTRA2 seems to write over ID_EXTRA1

所以,

i.putExtra(ID_EXTRA, new String[] { "1", "111"});

看起来不错,但是如何在第二个活动中从数组中提取值,我一直将其用于单个值.

Looks like the go but how do I extract the values from the array in the second activity, I have been using this for a single value.

passedVar = getIntent().getStringExtra(CoastList.ID_EXTRA);

我想我必须以某种方式将 ID_EXTRA 变成一个数组???

I guess I have to turn ID_EXTRA into an array somehow???

推荐答案

您可以使用多个键来传递多个值.而不是

You can pass multiple values by using multiple keys. Instead of

i.putExtra(ID_EXTRA, "1", "111");

i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");

当然,您必须为键定义 2 个常量,并且必须在新活动中分别读取.

Of course you have to define 2 constants for the keys and have to read both seperately in the new activity.

或者你可以通过

i.putExtra(ID_EXTRA, new String[] { "1", "111"});

这篇关于Android,我可以使用 putExtra 来传递多个值吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:26