1

我有两个活动说 X 和 y。在 x 中有 edittext n 6 单选按钮,如果用户单击按钮,则根据来自 edittext n 单选按钮的输入从数据库中检索值。这些值应显示在下一个活动 y 中。你可以帮忙提供片段吗..提前谢谢

4

3 回答 3

0

您应该将值放入一个包中,并将该包传递给启动下一个活动的意图。示例代码在这个问题的答案中: Passing a Bundle on startActivity()?

于 2012-09-27T17:08:13.603 回答
0

将您要发送的数据与您调用的 Intent 绑定,以转到下一个活动。

Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);

在 YourNextClass 活动中,您可以使用

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
    String data = extras.getString("yourKey");
    }
于 2012-09-27T17:35:02.043 回答
0

您可以使用 Bundle 或 Intent 轻松地将数据从一个活动传递到另一个活动。

让我们看一下使用 Bundle 的以下示例:

//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();

//This is where we put the data, you can basically pass any 
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");

//we put the bundle to the Intent
i.putExtra(b);

startActivity(i, 0);

在“NextActivity”中,您可以使用以下代码检索数据:

Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");

仅使用 Intent 传输数据怎么样。让我们看一下示例

Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);

在“NextActivity”中,您可以检索数据:

int highestScore = getIntent().getIntExtra("score");

现在你会问我,Intent 和 Bundle 之间有什么区别,它们似乎做的事情完全一样。

答案是肯定的,他们都做同样的事情。但是,如果您想传输大量数据、变量、大型数组,则需要使用 Bundle,因为它们有更多方法可以传输大量数据。(即,如果您只传递一个或两个变量,那么只需使用 Intent。

于 2012-09-27T17:38:16.210 回答