[英]Passing data from spinner to TextView depending on user selection
所以我有一個Spinner,其中包含2個可能的選項。 但是我在其他活動中只有一個TextView可以顯示用戶選擇。
問題是,如何在第二個活動中設置它? 使用各種if-語句嘗試了無數次,沒有任何效果。
這是我的微調器的代碼
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
intent.putExtra("ciljJePovecanjeTezine", cilj.getSelectedItem().toString());
} else if (position == 1){
intent.putExtra("ciljJeMrsavljenje", cilj.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
這是我要顯示文字的第二項活動
String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");
String prehrana2 = intent.getStringExtra("ciljJeMrsavljenje");
ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
ciljPrehranaRezultat.setText(prehrana);
ciljPrehranaRezultat.setText(prehrana2);
現在如何設置如果用戶選擇選項一,則TextView中將顯示選項一,如果用戶選擇選項二,則將出現選項二?
謝謝!!
您應該使用全局String變量來保存微調器值,然后按一下按鈕(或用於更改活動的任何方法),將其發送到其他活動。 例如:
String spinnerValue;
public void onCreate(...){
...
spinnerValue = cilj.getSelectedItem().toString()
...
}
public void onItemSelected(...){
spinnerValue = cilj.getSelectedItem().toString();
}
Intent intent = new Intent(THIS_ACTIVITY,OTHER_ACTIVITY.CLASS);
intent.putExtra("spinnerValue",spinnerValue);
startActivity...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.