2014-05-25 64 views
0

在將多個數據(在本例中爲三個數組(兩個int,一個字符串))發送到第二個活動頁面時存在問題。單個活動中的多個意圖

我不確定這是如何完成的。我想知道的是如何在一個Start Activity方法中發送這些數組,如果可能的話。我當前的代碼是:

public void onClickGoToTeamSummary(View view) 
{ 
    Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class); 
    Intent intentTeamPoints = new Intent(MainActivity.this, ResultsActivity.class); 
    Intent intentTeamGoals = new Intent(MainActivity.this, ResultsActivity.class); 

    intentTeamNames.putExtra("footballClubs", myTeams); 
    intentTeamPoints.putExtra("clubPoints", pointsAttained); 
    intentTeamGoals.putExtra("clubGoals", goalsScored); 

    startActivity(intentTeamPoints); 
    startActivity(intentTeamNames); 
    startActivity(intentTeamGoals); 
} 

我曾嘗試:

startActivity(intentTeamPoints, intentTeamNames, intentTeamGoals); 

無濟於事。爲了幫助,我的下一個活動getIntent看起來是這樣的:

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_results); 

    Intent intentClubNames = getIntent(); 
    String[] club_names = intentClubNames.getStringArrayExtra("footballClubs"); 

    Intent intentClubPoints = getIntent(); 
    int[] team_points = intentClubPoints.getIntArrayExtra("clubPoints"); 

    Intent intentTeamGoals = getIntent(); 
    int[] club_goals = intentTeamGoals.getIntArrayExtra("clubGoals"); 
} 

代碼本身的作品只提供一個startActivity被使用。我想知道如何通過一項活動將我的所有數組傳遞到第二個活動頁面,如果有人能幫助我的話。

+0

我認爲這一個是在這裏明確解釋http://stackoverflow.com/questions/18195734/sending-multiple-intents-from-a-single-activity-to-another-activity – mahbubcsedu

回答

1

試試這個..

您可以發送單Intent

public void onClickGoToTeamSummary(View view) 
{ 
    Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class); 

    intentTeamNames.putExtra("footballClubs", myTeams); 
    intentTeamNames.putExtra("clubPoints", pointsAttained); 
    intentTeamNames.putExtra("clubGoals", goalsScored); 

    startActivity(intentTeamNames); 
} 

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_results); 

    Intent intentClubNames = getIntent(); 
    String[] club_names = intentClubNames.getStringArrayExtra("footballClubs"); 
    int[] team_points = intentClubNames.getIntArrayExtra("clubPoints"); 
    int[] club_goals = intentClubNames.getIntArrayExtra("clubGoals"); 
} 
+0

工作完美。沒有意識到你可以把多個價值放在一個單一的意圖。再次感謝。 – wilbomc

1

所有值不要使用多重Intents但多個羣衆演員。

// create your Intent as normal 
Intent myIntent = new Intent(MainActivity.this, ResultsActivity.class); 

// then you can add multiple extras 
myIntent.putExtra("footballClubs", myTeams); 
myIntent.putExtra("clubPoints", pointsAttained); 
myIntent.putExtra("clubGoals", goalsScored); 
startActivity(myIntent); 

然後接收它們將是相同的。您只會收到一個Intent,並使用正常的密鑰作爲每個額外的Array

相關問題