【问题标题】:pass value from 1st to third activity in android在android中将值从第一个活动传递到第三个活动
【发布时间】:2012-12-08 23:27:16
【问题描述】:

我已经开发了一个安卓应用程序。 在这里,我必须将值从第一个活动传递到第三个活动。

成功开发了从第一个活动传递到第二个活动的值。但是我无法将第二个活动的值传递到第三个活动。请帮助我如何开发这些。请帮助我。

这是我的第一个活动:

     Intent i = new Intent(getApplicationContext(), CustomerLogin.class);
                i.putExtra("GrandTotal", mGrandTotal);
                startActivity(i);

第二个活动:

Intent in = getIntent();
Bundle b = getIntent().getExtras();
String total = b.getString("GrandTotal");
TextView grandtotal = (TextView) findViewById(R.id.grand_total);
 grandtotal.setText("Welcome ," + total );

这里的值是从第一个活动成功传递到第二个活动。 现在我必须将这些值传递给第三个活动。我该怎么做。

现在这是我的第二个活动:

if(isUserValidated && isPasswordValidated)
{
    String s= getIntent().getStringExtra(total);
    Intent intent = new Intent(CustomerLogin.this,PayPalIntegrationActivity.class);
    intent.putExtra("GrandTotal", s);
    intent.putExtra("login",username.getText().toString());
    startActivity(intent);

}

这是我的第三个活动:

    Bundle b = getIntent().getExtras();

    String total = b.getString("GrandTotal");
    TextView grandtotal = (TextView) findViewById(R.id.check);
    grandtotal.setText("Welcome ," + total );

现在我必须运行应用程序,这意味着我正在获得第二个活动的总值。但我没有得到第三个活动的总值。请帮助我。这里做错了什么。

【问题讨论】:

标签: java android android-intent bundle pass-data


【解决方案1】:
 Intent i = new Intent(getApplicationContext(), CustomerLogin.class);
 i.putExtra("GrandTotal", mGrandTotal);
 startActivity(i);

第二个活动:

Bundle b = getIntent().getExtras();
String total = b.getString("GrandTotal");
TextView grandtotal = (TextView) findViewById(R.id.grand_total);
grandtotal.setText("Welcome ," + total );   

  if(isUserValidated && isPasswordValidated)  {      
      Intent intent = new Intent(CustomerLogin.this,PayPalIntegrationActivity.class);
      intent.putExtra("GrandTotal", total);
      intent.putExtra("login",username.getText().toString());
      startActivity(intent);    
    }

这是我的第三个活动:

Bundle b = getIntent().getExtras();    
String total = b.getString("GrandTotal");
String name = b.getString("login");
TextView grandtotal = (TextView) findViewById(R.id.check);
grandtotal.setText("Welcome ," + total );

【讨论】:

  • 空值只在此处显示从第 2 到第 3。这是我的第 2 活动代码:pastie.org/5561078
  • 检查你的代码一旦“if”条件正在执行或者正在执行
  • 如果条件只在我的代码中执行。在第三次活动中获得 null
  • 这是我的第一个活动代码:pastie.org/5564017 这是我的第二个活动代码:pastie.org/5564018 这是我的第三个活动代码:pastie.org/5564020 请检查我的代码并给我解决方案。我已更改我的 code.code 只是正确的。但我得到的是 null 值。请检查它。
  • 我必须在公共上创建字符串总数。总计 = b.getString("GrandTotal");现在我得到了 o/p。谢谢你的帮助
【解决方案2】:

使用简单的静态变量。或为此目的共享首选项。

【讨论】:

    【解决方案3】:
    if(isUserValidated && isPasswordValidated)
    {
      String s= getIntent().getStringExtra(total);// problem is here
      ....// In second activity
    }
    

    更改为String s= getIntent().getStringExtra("GrandTotal");

    【讨论】:

    • 我只在第二次到第三次活动中获得空值。这是我的第二次活动代码:pastie.org/5561078。请检查我的代码并为我提供解决方案。
    【解决方案4】:

    替换

     if(isUserValidated && isPasswordValidated)
        {
            String s= getIntent().getStringExtra(total); // Replace this line
            Intent intent = new Intent(CustomerLogin.this,PayPalIntegrationActivity.class);
            intent.putExtra("GrandTotal", s);
            intent.putExtra("login",username.getText().toString());
            startActivity(intent);
    
        }
    

    与

    String s= getIntent().getStringExtra("GrandTotal");
    

    【讨论】:

    • 我只在第二次到第三次活动中获得空值。这是我的第二次活动代码:pastie.org/5561078。请检查我的代码并为我提供解决方案。
    【解决方案5】:

    如果您需要传递完全相同的值,您可以使用:

    Intent intent = new Intent(CustomerLogin.this,PayPalIntegrationActivity.class);
    intent.putextras(this.getIntent.getExtras());
    

    【讨论】:

    • 这很简单。它旨在放入 Activity 2。它将 Activity 1 给出的附加内容放入 Activity 3 的 Intent 中。它假设您不想更改附加内容。
    【解决方案6】:

    在你的第三活动

    中试试这个

    编辑:

    if(getIntent().getStringExtra("GrandTotal")!= null)
    {
          String total =getIntent().getStringExtra("GrandTotal"); <---------------
          TextView grandtotal = (TextView) findViewById(R.id.check);
          grandtotal.setText("Welcome ," + total );
    }
    

    【讨论】:

    • 我只在第二次到第三次活动中获得空值。这是我的第二次活动代码:pastie.org/5561078。请检查我的代码并为我提供解决方案。
    【解决方案7】:

    对于这个更好的解决方案是在 android 中使用共享首选项。 将您的价值存储在共享偏好中,并在第三个活动中获取该价值并根据您的要求使用它。

    更多详情:Example of Shared Preference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 2012-08-30
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      相关资源
      最近更新 更多