2012-09-12 84 views
1

我想從一個活動發送2參數到另一個,但由於某種原因只有一個參數傳遞。會是什麼呢?意圖不通過我的數據

發送活動:

showActivity(OrderActivity.class, new Pair("CUSTOMER_ID", customerId), new Pair("CUSTOMER_TYPE", customerType)); 

接收活動:客戶ID是顯示0,但數據類型是得了

Bundle extras = getIntent().getExtras(); 

    long customerId = extras.getLong("CUSTOMER_ID"); 
    int customerType = extras.getInt("CUSTOMER_TYPE"); 
    Log.d("===customer ", " id : " + customerId + " type : " + customerType); 

showActivity看起來像這樣

protected void showActivity(Class<? extends BaseKaizenActivity> clazz, Pair<String, Object> ... parameters) { 
    Intent intent = new Intent(this, clazz); 
    if (parameters != null) { 
     for (Pair<String, Object> pair : parameters) { 
      if (pair.second instanceof Integer) { 
       intent.putExtra(pair.first, (Integer)pair.second); 
      } 
      else if (pair.second instanceof Parcelable) { 
       intent.putExtra(pair.first, (Parcelable)pair.second); 
      } 
     } 
    } 
    startActivity(intent); 
} 
+2

發送發送活動中發送數據的完整代碼? – Praveenkumar

+0

Y你不能簡單地使用getExtra和putExtra? –

+0

你能發表showActivity()的細節嗎? – topxebec

回答

1
if (pair.second instanceof Integer) { 
       intent.putExtra(pair.first, (Integer)pair.second); 
      } 
      else if (pair.second instanceof Parcelable) { 
       intent.putExtra(pair.first, (Parcelable)pair.second); 
      } 
看起來像

就像你霧寫一條龍的條款。如果沒有,如果沒有,也沒有提出額外的數據。

+0

的類型有關,因此它就像這樣。 '如果(pair.second的instanceof整數){ \t \t \t \t \t intent.putExtra(pair.first,(整數)pair.second); \t \t \t \t \t} \t \t \t \t否則如果(pair.second的instanceof龍){ \t \t \t \t \t intent.putExtra(pair.first,(長)pair.second); \t \t \t \t \t} \t \t \t \t否則如果(pair.second的instanceof Parcelable){ \t \t \t \t \t intent.putExtra(pair.first,(Parcelable)pair.second); \t \t \t \t} – ZAJ

+0

是的,我認爲是。 – topxebec

0

你爲什麼用這個?只是使用put和get意思的方法其非常簡單。

intent.putExtra("backPressed", true); 
getIntent().getBooleanExtra("backPressed", false); 

這是我的工作代碼,沒有問題。