2012-10-27 41 views
0

我是Android開發的新手,並面臨一些意圖問題。 我有一個項目列表顯示在屏幕上,並希望通過點擊列表中的任何項目來調用不同的活動。意圖不正確地傳遞數據

我使用下面的代碼調用列表項的點擊不同的活動 -

@Override 
    protected void onListItemClick(ListView lv, View view, int position, long id){ 
     super.onListItemClick(lv, view, position, id); 
     //code to call activity to edit the task 
     Intent intent = new Intent(this, ReminderModificationActivity.class); 
     intent.putExtra("RowId", id); 
     Log.i(TAG, "row clickd --> " + id); 
     startActivityForResult(intent, ACTIVITY_EDIT); 

    } 
在這上面的代碼

,我得到的列表項的正確的ID點擊爲1,2或3。在「ReminderModificationActivity」,我有這樣的一段代碼在我的onCreate()函數 - 在Log.i收到

Log.i(TAG, "getIntent --> " + getIntent().getExtras().getInt("RowId")); 

     //code to check what row id have been passed 
     if(getIntent() != null) {           
      Bundle extras = getIntent().getExtras();      
      int mRowId = extras != null ? extras.getInt("RowId") : -1;  
      // Do stuff with the row id here 
      if(mRowId != -1){ 
       //code if RowId valid 

      } 
     } 

值始終爲0誰能幫我就什麼,我在這裏失蹤?請詳細說明,因爲我對這個平臺完全陌生。

ans - int 同時捕獲intents。

由於事先 雷

回答

0

你爲什麼不使用的經過很長的值,而不是捆綁的Intent.putExtra?

,那麼你只是做一個

intent.putExtra("RowId", id); 

和檢索室內用一個

long incomingID = getLongExtra("RowId", -1); 

和方式,儘量只要治療中的代碼兩件

+0

我做了一些數據愚蠢的錯誤.. :( – RayKaushik

+1

所以我解決了它,太愚蠢,但我犯了兩個錯誤 - 1.我創建了一個新的變量mROWiD,而我已經有一個全局變量。 'int mRowId = extras!= null? extras.getInt(「RowId」):-1;' 2.我需要使用getLong而不是getInt。感謝@Thomas爲此。 謝謝你們.. !! – RayKaushik