2015-04-29 46 views
0

我想從一個活動傳遞三個消息到另一個。但Android Studio只查看最後創建的「putExtra」並將其應用於所有輸出。我的代碼如下。Android應用程序只讀最後一個額外的消息

第一項活動,其中創建意圖:

 public void buttonOnClick_Goals(View v) { 

    Intent intent = new Intent(v.getContext(), Goals.class); 

    EditText benchpress = (EditText) findViewById(R.id.txtBenchpress); 
    String benchpress_message = benchpress.getText().toString(); 
    intent.putExtra(BENCH_PR, benchpress_message); 

    EditText squat = (EditText) findViewById(R.id.txtSquat); 
    String squat_message = squat.getText().toString(); 
    intent.putExtra(SQUAT_PR, squat_message); 

    EditText deadlift = (EditText) findViewById(R.id.txtDeadlift); 
    String deadlift_message = deadlift.getText().toString(); 
    intent.putExtra(DEAD_PR, deadlift_message); 

    startActivity(intent); 
} 

次活動,其中的文本字段設置爲傳遞的消息:

 protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_goals); 
    Intent intent = getIntent(); 
    String bench_message = intent.getStringExtra(PersonalRecords.BENCH_PR); 
    String squat_message = intent.getStringExtra(PersonalRecords.SQUAT_PR); 
    String dead_message = intent.getStringExtra(PersonalRecords.DEAD_PR); 

    TextView benchText = (EditText)findViewById(R.id.txtbenchpr); 
    benchText.setText(bench_message, TextView.BufferType.EDITABLE); 

    TextView squatText = (EditText)findViewById(R.id.txtsquatpr); 
    squatText.setText(squat_message, TextView.BufferType.EDITABLE); 

    TextView deadText = (EditText)findViewById(R.id.txtdeadpr); 
    deadText.setText(dead_message, TextView.BufferType.EDITABLE); 

} 
+1

唯一需要檢查的是「PersonalRecords.BENCH_PR」,「PersonalRecords.SQUAT_PR」和「PersonalRecords.DEAD_PR」的值。 – user370305

+1

最有可能你的3個額外的鍵具有相同的值,在這種情況下,它們是相同的額外的,並且只有實際發送的最後一個值。 –

回答

1

確保BENCH_PR,SQUAT_PR和DEAD_PR有不同的值!

相關問題