2016-07-26 198 views
-2

我試圖從一個活動類(A)傳遞一個int變量到另一個活動類(B)。因爲我是初學者,所以我被困在這裏。從一個類的int變量傳遞值到另一個類

我已經嘗試了許多方法,但每個I調試模式運行的代碼時,我可以看到,這是我傳遞到第二類的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*********************** 

} 
+5

你可以發佈代碼也爲我們找到確切的問題。 – VatsalSura

+0

您是否在使用意向? –

回答

2

您需要使用意圖: 活動答:

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 
+0

我不能使用'startActivity(intent);'因爲我不想在價值通過後立即開始活動B.爲此目的有一個特殊的專用DONE按鈕。我怎樣才能做到這一點? –

+0

您將代碼放入完成按鈕的OnClick方法中 – Chol

+0

仍然在達到第二個活動時該值變爲0。 我已經用代碼更新了問題。 –

1

您可以隨時使用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或直接查找你的問題ctly在所以,你的問題是一個相當普遍的一個,你可以閃避次活動

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

我相信'getIntExtra'有一個額外的參數,不是? –

+0

沒錯,我會在這裏編輯我的回答 –

+0

Noob。搜索了很多問題。但明白這個錯誤在我身邊。道歉! –

0
Intent myIntent = new Intent(FirstActivity.this,SecondActivity.class); 
myIntent.putExtra("value", (int)100); 
startActivity(myIntent); 

在你當前活動,創建一個新的意圖:

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

然後在新的活動,檢索這些值:

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

使用此技巧將變量從一個活動傳遞給另一個活動。

0

是反對票..

1

爲此,您應該使用意向。你可以從下面的代碼中獲得想法;

假設你的int變量爲;

int x = 2; 

從那裏你想要去到另一個活動將這個代碼的活動:

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

在第二個活動使用此代碼;

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

您可以使用Shared首選項來處理此問題。

在活動A:

INT沒有= 1; SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);

Editor editor = sharedpreferences.edit();

editor.putString(「my_number」,no +「」);

editor.commit();

在活性B:

SharedPreferences首選項= getSharedPreferences(MyPREFERENCES,MODE_PRIVATE);

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

相關問題