【问题标题】:Pass value from an int variable in one class to another class将值从一个类中的 int 变量传递到另一个类
【发布时间】:2016-11-29 22:14:09
【问题描述】:

我试图将一个 int 变量从一个活动类 (A) 传递到另一个活动类 (B)。由于我是初学者,所以我被困在这里。

我尝试了很多方法,但是每次在调试模式下运行代码时,我都可以看到我传递给第二个类的 int 值变成了 0。

活动 A 的代码

   public void startMainActivity(View view)
   {
       //Sending the semester value to Navdrawer
       Intent intentsem = new Intent(LoginActivity.this, NavDrawer.class);
       intentsem.putExtra("UserSemester", usersemester);
       startActivity(intentsem);


       //Sending the branch value to Navdrawer

       Intent intentbranch= new Intent(LoginActivity.this, NavDrawer.class);
       intentbranch.putExtra("UserBranch", userbranch);
       startActivity(intentbranch);



   }

活动 B 的代码

public void startSyllabusactivity(View view)

{
    // for first year

    int semester = getIntent().getIntExtra("UserSemester", 0);
    if (semester==1 || semester==2) {
        Intent l = new Intent(this, firstyear_syllabussubject_selector.class);
        startActivity(l);
    }


    //rest of the actions with branches to be added***********************

}

【问题讨论】:

  • 您能否将代码也发布给我们,以便我们找到您的确切问题。
  • 你有没有使用意图?

标签: android android-intent


【解决方案1】:

你需要使用 Intent: 活动 A:

doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 Intent intent = new Intent(context, ActivityB.class);
                 intent.putExtra("my_value", 2); //2 is the value you want to pass
                 intent.putExtra("UserBranch", userbranch);
                 startActivity(intent);

            }
        });

活动 B:

int variable = getIntent().getIntExtra("my_value", 0);//0 is the default value if the getintExtra doesn't find the "my_value"
int userBranch= getIntent().getIntExtra("UserBranch", 0);//0 is the default 

【讨论】:

  • 我不能使用'startActivity(intent);'因为我不想在传递值后立即启动活动 B。为此,有一个专用的 DONE 按钮。我怎样才能做到这一点?
  • 您将代码放在完成按钮的 OnClick 方法中
  • 到达第二个活动时该值仍然变为0。我已经用代码更新了问题。
  • 在代码中,您似乎两次启动了相同的活动
  • 在第二个中,您要放置 intentbranch.putExtra("UserBranch", userbranch);所以尝试使用“UserBranch”来获取 int 而不是“UserSemester”
【解决方案2】:

为此,您应该使用 Intent。你可以从下面的代码中得到想法;

假设你有 int 变量为;

int x = 2;

把这段代码放在你想去另一个Activity的Activity中:

Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("xVar", x);
startActivity(i);

在第二个活动中使用此代码;

Intent i = getIntent();
int xVal = i.getIntExtra("xVar", 0);

【讨论】:

    【解决方案3】:

    您总是可以使用intent.putExtra() 方法,例如:

    Intent intent = new Intent(ActivityA.this, ActivityB.class);
    intent.putExtra("myInt", myInt);
    startActivity(intent);
    

    通过ActivityB获取它:

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      int value = extras.getInt("myInt");
      //The key argument here must match that used in the other activity
    }
    

    提醒一下,您应该先在 Google 上或直接在 SO 上查找您的问题,因为您的问题是一个非常笼统的问题,您可以避免投反对票。..

    【讨论】:

    • 我相信getIntExtra 有一个额外的参数,不是吗?
    • 这里是菜鸟。搜索了很多问题。但明白错误在我这一边。道歉!
    【解决方案4】:
    Intent myIntent = new Intent(FirstActivity.this,SecondActivity.class);
    myIntent.putExtra("value", (int)100);
    startActivity(myIntent);
    

    在第二个活动中

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

    【讨论】:

      【解决方案5】:

      在你当前的 Activity 中,创建一个新的 Intent:

      Intent i = new Intent(CurrentActivity.this, NewActivity.class);
      i.putExtra("intValue",100);
      startActivity(i);
      

      然后在新的 Activity 中,检索这些值:

      Bundle extras = getIntent().getExtras();
      if (extras != null) {
        int value = extras.getInt("intValue");
        //The key argument here must match that used in the other activity
      }
      

      使用这种技术将变量从一个 Activity 传递到另一个。

      【讨论】:

        【解决方案6】:

        您可以使用共享首选项来解决此问题。

        在活动 A 中:

        int no=1;

        SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        编辑器 editor = sharedpreferences.edit();

        editor.putString("my_number", no+"");

        editor.commit();

        在活动 B 中:

        SharedPreferences 首选项 = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);

        String m_no = prefs.getString("my_number", null);

        【讨论】:

          猜你喜欢
          • 2013-02-08
          • 2016-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多