【问题标题】:android integer value passing from one activity to other issueandroid整数值从一个活动传递到另一个问题
【发布时间】:2015-09-14 04:41:46
【问题描述】:

我想将整数值从一个活动传递给另一个我尝试这样做但值不通过请告诉我正确的解决方案。

这是我想将值传递给其他活动的活动代码。

Intent intent = new Intent(getApplicationContext(),
                            TotalCalories.class);
                    intent.putExtra("Total Sum", sum);
                    startActivity(intent);

这是我想要获取值的代码。

  Bundle extras = getIntent().getExtras();
    int sum = Integer.parseInt(extras.getString("Total Sum"));
    TextView textview = (TextView) findViewById(R.id.textViewname);
    textview.setText(extras.getString("Total Sum"));

【问题讨论】:

  • 你从一个活动传递值的字符串,你在下一个活动中没有使用相同的字符串

标签: android android-activity integer


【解决方案1】:

尝试像这样更改 get String 中的 Totalsum

 Intent intent = new Intent(getApplicationContext(),
                                    TotalCalories.class);
                            intent.putExtra("TotalSum", sum);
                            startActivity(intent);

在新活动的 Oncreate 中添加这个

  Bundle extras = getIntent().getExtras();
            int sum = Integer.parseInt(extras.getString("TotalSum"));
            TextView textview = (TextView) findViewById(R.id.textViewname);
            textview.setText("Your OutPut"+sum);

【讨论】:

【解决方案2】:

来自发件人活动:

Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);

在接收器活动中:

 Intent mIntent = getIntent();
 int intValue = mIntent.getIntExtra("intVariableName", 0);

注意:接收到的第二个参数'0',是默认值。如果您没有通过intent 传递任何值,那么它会将0 分配给intValue 变量。

【讨论】:

  • 如何在 textview 中显示值
  • 首先创建一个textView对象:TextView tv = (TextView)findViewById(...)然后tv.setText(""+intValue);
【解决方案3】:

在另一个活动中使用以下来获取整数

Intent i =getIntent();
int abc=  i.getIntExtra("TotalSum");
TextView textview =   (TextView)findViewById(R.id.textViewname);
textview.setText(String.valueOf(abc));

【讨论】:

  • 最后一行应该写什么
【解决方案4】:

您的代码中有两个问题

1) 你不是在传递 bundle,而是从 bundle 中获取值。

使用下面的链接 How to pass a value from one Activity to another in Android?

2) 并获得整数值

Intent in =getIntent();
int abc=  in.getIntExtra("value");

【讨论】:

    【解决方案5】:

    这就是你的做法

    int yourInt = 0; //initialize it
    
    yourInt = yourIntergerValue;
    
    Intent yourActivityIntent = new Intent(getApplicationContext(),YourActivity.class); 
    yourActivityIntent.putExtra("yourIntKey",yourInt); 
    startActivity(yourActivityIntent);
    

    【讨论】:

      【解决方案6】:

      先把值放到FirstActivity中

             Intent intent = new Intent(this, yourSecondActivity.class);
             intent.putExtra("intValue",value);
             startActivity(intent);
      

      那么, SecondActivity 获取值

              Bundle exBundle= getIntent().getExtras();
              int intValue= exBundle.getInt("intValue");
              TextView textview = (TextView) findViewById(R.id.textViewname);
              textview.setText("Your Value is "+intValue);
      

      【讨论】:

        猜你喜欢
        • 2011-10-27
        • 1970-01-01
        • 2018-05-31
        • 2021-03-15
        • 1970-01-01
        • 2015-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多