将2个微调器值从活动发送到另一个

时间:2019-01-27 18:35:57

标签: java android android-intent

我在spinner中有2个Blankfragment.java,我想发送密钥intent.putExtra(Category1)intent.putExtra(Category2) 并在同一活动aaa.java

中接收它们

by getIntent().getStringExtra("Category1");或getIntent().getStringExtra("Category2")(如果使用切换大小写或if语句)

但是我不能这样做,因为在getStringExtra中不能使用多个aaa.java

请帮助我

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)
           ) 
   ...
   .....
   .......
   .........

1 个答案:

答案 0 :(得分:0)

更改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
   }
}