[英]send 2 spinner values from activity to another
我在Blankfragment.java
有 2 個spinner
,我想發送intent.putExtra(Category1)
或intent.putExtra(Category2)
的密鑰,並在同一活動aaa.java
接收它們
通過getIntent().getStringExtra("Category1")
; 或getIntent().getStringExtra("Category2")
在 switch case 或 if 語句中
但我不能這樣做,因為我不能在aaa.java
使用多個getStringExtra
請幫我
Blankfragment.java 的代碼:
int spinner_pos = spinner.getSelectedItemPosition();
int spinner2_pos = spinner2.getSelectedItemPosition();
if (spinner_pos == 0 && spinner2_pos == 0) {
Intent intent = new Intent(getActivity(), aaa.class);
String a1 = null;
intent.putExtra("Category1", a1 );
startActivity(intent);
}
else if (spinner_pos == 0 && spinner2_pos == 1) {
Intent intent = new Intent(getActivity(), aaa.class);
String a2 = null;
intent.putExtra("Category2", a2);
startActivity(intent);
}
aaa.java 的代碼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aaa);
.
..
...
//need to use
getIntent().getStringExtra("Category1"); .. // for if -> "01"
and
getIntent().getStringExtra("Category2"); .. // for else if -> "02"
//in this if statment
if(// write something) {
loadListWorkers("01");
}
else if (//write something) {
loadListWorkers("02");
}
}
private void loadListWorkers(String placeId) {
adapter = new FirebaseRecyclerAdapter<workers, WorkerViewHolder>
( workers.class
, R.layout.vh_worker_item
, WorkerViewHolder.class
, workerList.orderByChild("Worker_place_ID").equalTo(placeId)
)
...
.....
.......
.........
更改aaa.java
的邏輯。 檢查Incoming Intent
是否不為空。
Intent cameFrom = getIntent();
if(cameFrom != null){
//Here your intent isn't null now let's check for the values
if(cameFrom.hasExtra("Category1")){
cameFrom.getStringExtra("Category1");
}else if(cameFrom.hasExtra("Category2")){
cameFrom.getStringExtra("Category2");
}else{
//Nothing Found
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.