2017-03-03 56 views
0

在我的應用程序中,我在一個活動中使用多個微調器,一切工作正常,但是當我根據當前活動選定的微調項目開始另一個活動時, spinners選擇的項目本身會改變其「0」索引值。當我從下一個活動返回到帶有旋轉器的活動時(即活動恢復時),其選定值將設置爲「0」索引。如何在開始另一項活動時阻止更改選定項目的紡紗工的這種行爲。Android微調選定項目獲取下一個活動開始時的變化

請安卓高手儘早做出迴應!我有點匆忙。

onSavedInstanceStateMethod保存微調的指標值

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putInt("InterestSpinner", interest.getSelectedItemPosition()); 
    outState.putInt("CompanySpinner", company.getSelectedItemPosition()); 
    outState.putInt("PolicySpinner", policy.getSelectedItemPosition()); 
    outState.putInt("DeptSpinner", dept.getSelectedItemPosition()); 
} 

這裏是我如何把一些字符串意圖後調用另一個活動

Intent intent = new Intent(PolicySelection.this, TakePicture.class); 

       intent.putExtra("INTERESTID", interestId); 
       intent.putExtra("COMPANYID", companyId); 
       intent.putExtra("TRANSID", transID); 
       intent.putExtra("UserName", username); 
       intent.putExtra("Password", password); 
       intent.putExtra("ClaimID", ""); 
       intent.putExtra("OP", "UNDERWRITING"); 

       startActivity(intent); 

這是我的一個微調onItemChangeListener()方法`

company.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) { 
      popDeptSpinner(); 
      popPolicySpinner(); 
      popInterestSpinner(); 
     } 

     public void onNothingSelected(AdapterView<?> arg0) { 
     } 
    });` 

這是我的onCreate方法

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.policy_selection); 

    policy = (Spinner) findViewById(R.id.policySelectionSpinner); 
    company = (Spinner) findViewById(R.id.companySelectSpinner); 
    interest = (Spinner) findViewById(R.id.interestIdSpinner); 
    department = (Spinner) findViewById(R.id.deptSpinner); 
    camera = (Button) findViewById(R.id.camera); 
    damages = (Button) findViewById(R.id.damages); 
    upload = (Button) findViewById(R.id.upload); 
    showDetail = (Button) findViewById(R.id.showDetail); 
    title = (TextView) findViewById(R.id.title); 


    popCompanySpinner(); 

    popDeptSpinner(); 

    popPolicySpinner(); 

    popInterestSpinner(); 


    showDetail.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      boolean ready = isNetworkAvailable(); 
      if (ready) { 
       if (policy.getAdapter().getCount() > 0) { 
        deptID = department.getSelectedItem().toString(); 
        if (deptID.equals("MOTOR") || deptID.equals("MISCELLEANEOUS")) { 
         interestId = interest.getSelectedItem().toString(); 
         transID = transId_list.get(getPositions(policy.getSelectedItem().toString())); 
         branchId = branchId_list.get(getPositions(policy.getSelectedItem().toString())); 
         deptNo = deptNoList.get(getPositions(policy.getSelectedItem().toString())); 

         Intent intent = new Intent(PolicySelection.this, InterestDetail.class); 

         intent.putExtra("UserName", username); 
         intent.putExtra("Password", password); 
         intent.putExtra("InterestID", interestId); 
         intent.putExtra("DeptID", deptNo); 
         intent.putExtra("TransID", transID); 

         startActivity(intent); 

        } else { 
         Toast.makeText(PolicySelection.this, "No Details for Current Policy!", Toast.LENGTH_SHORT).show(); 
        } 
       } else { 
        Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show(); 
       } 
      } else { 
       Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    }); 

    damages.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      boolean ready = isNetworkAvailable(); 
      if (ready) { 
       if (policy.getAdapter().getCount() > 0) { 
        if (department.getSelectedItem().toString().equals("MOTOR")) { 
         Integer pos; 
         pos = getPositions(policy.getSelectedItem().toString()); 
         switch (company.getSelectedItem().toString()) { 
          case "UIC": 
           transID = transId_list.get(pos); 
           companyId = "UIC"; 
           surveyTransID = UICSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString())); 
           break; 
          case "UICTAK": 
           transID = transId_list.get(pos); 
           companyId = "UICTAK"; 
           surveyTransID = UICTAKSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString())); 
           break; 
          case "SPI": 
           transID = transId_list.get(pos); 
           companyId = "SPI"; 
           surveyTransID = SPISurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString())); 
           break; 
          case "SPITAK": 
           transID = transId_list.get(pos); 
           companyId = "SPITAK"; 
           surveyTransID = SPITAKSurveyTransID.get(getInterestIdPositions(interest.getSelectedItem().toString())); 
           break; 
          default: 
           break; 
         } 

         Intent intent = new Intent(PolicySelection.this, DamagesDetail.class); 

         if (Objects.equals(transID, "")) { 
          company.getSelectedItem().toString(); 
         } else { 

          String options = new WebServiceCall().getPreInspectionOptions("GetPreInspectionOptions", username, password, companyId, transID); 

          intent.putExtra("TRANSID", transID); 
          intent.putExtra("COMPANYID", companyId); 
          intent.putExtra("UserName", username); 
          intent.putExtra("Password", password); 
          intent.putExtra("Options", options); 
          intent.putExtra("SurveyTransID", surveyTransID); 
         } 

         startActivity(intent); 

        } else { 
         Toast.makeText(PolicySelection.this, "No Damages Form for Current Policy!", Toast.LENGTH_SHORT).show(); 
        } 
       } else { 
        Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show(); 
       } 

      } else { 
       Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

    upload.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      boolean ready = isNetworkAvailable(); 
      if (ready) { 
       if (policy.getAdapter().getCount() > 0) { 

        Integer pos; 
        pos = getPositions(policy.getSelectedItem().toString()); 

        switch (company.getSelectedItem().toString()) { 
         case "UIC": 
          transID = transId_list.get(pos); 
          branchId = branchId_list.get(pos); 
          break; 
         case "UICTAK": 
          transID = transId_list.get(pos); 
          branchId = branchId_list.get(pos); 
          break; 
         case "SPI": 
          transID = transId_list.get(pos); 
          branchId = branchId_list.get(pos); 
          break; 
         case "SPITAK": 
          transID = transId_list.get(pos); 
          branchId = branchId_list.get(pos); 
          break; 
         default: 
          break; 
        } 

        Intent intent = new Intent(PolicySelection.this, SendInfoForm.class); 

        intent.putExtra("USERNAME", username); 
        intent.putExtra("PASSWORD", password); 
        intent.putExtra("COMPANYID", company.getSelectedItem().toString()); 
        intent.putExtra("LAT", "1"); 
        intent.putExtra("LONG", "1"); 
        intent.putExtra("DEPTID", department.getSelectedItem().toString()); 
        intent.putExtra("INTERESTID", interest.getSelectedItem().toString()); 
        intent.putExtra("BRANCHID", branchId); 
        intent.putExtra("TRANSID", transID); 
        intent.putExtra("OP", "UNDERWRITING"); 
        intent.putExtra("CLAIMID", ""); 

        startActivity(intent); 
       } else { 
        Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show(); 
       } 

      } else { 
       Toast.makeText(PolicySelection.this, "Internet Connectivity Problem!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

    camera.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if (policy.getAdapter().getCount() > 0) { 

       Integer pos; 
       pos = getPositions(policy.getSelectedItem().toString()); 

       if (policyList != null) { 
        if (Objects.equals(SelectedPolicy, " ")) { 
         SelectedPolicy = company.getSelectedItem().toString(); 
        } 
       } 
       switch (company.getSelectedItem().toString()) { 
        case "UIC": 
         interestId = interest.getSelectedItem().toString(); 
         companyId = "UIC"; 
         transID = transId_list.get(pos); 
         break; 
        case "UICTAK": 
         interestId = interest.getSelectedItem().toString(); 
         companyId = "UICTAK"; 
         transID = transId_list.get(pos); 
         break; 
        case "SPI": 
         interestId = interest.getSelectedItem().toString(); 
         companyId = "SPI"; 
         transID = transId_list.get(pos); 
         break; 
        case "SPITAK": 
         interestId = interest.getSelectedItem().toString(); 
         companyId = "SPITAK"; 
         transID = transId_list.get(pos); 
         break; 
        default: 
         break; 
       } 

       Intent intent = new Intent(PolicySelection.this, TakePicture.class); 

       intent.putExtra("INTERESTID", interestId); 
       intent.putExtra("COMPANYID", companyId); 
       intent.putExtra("TRANSID", transID); 
       intent.putExtra("UserName", username); 
       intent.putExtra("Password", password); 
       intent.putExtra("ClaimID", ""); 
       intent.putExtra("OP", "UNDERWRITING"); 

       startActivity(intent); 

      } else { 
       Toast.makeText(PolicySelection.this, "No Policy Exists in Company! ", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 

    company.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) { 
      popDeptSpinner(); 
      popPolicySpinner(); 
      popInterestSpinner(); 
     } 

     public void onNothingSelected(AdapterView<?> arg0) { 
     } 
    }); 

    policy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) { 
      if (policyList != null) { 
       SelectedPolicy = policy.getSelectedItem().toString(); 
       policy.setSelection(position); 
       popInterestSpinner(); 
      } 
     } 

     public void onNothingSelected(AdapterView<?> arg0) { 
     } 
    }); 

    interest.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      if (interestList != null) { 

      } 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> adapterView) { 

     } 
    }); 

    department.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      popPolicySpinner(); 
      popInterestSpinner(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> adapterView) { 

     } 
    }); 


} 
+0

請把代碼片段 –

+0

張貼在其中添加紡紗您的活動代碼。你有onResume()這個活動? –

+0

@Rajesh Kushvaha代碼在這裏發佈太久了。我的實際問題是,當我運行另一個活動時,spinners項目所選項目將回到其初始狀態 –

回答

0

如何使用SaveInstanceState

@Override 
public void onSaveInstanceState(Bundle outState) {//store your instance here 
    outState.putString("message", "This is my message to be reloaded"); 
    super.onSaveInstanceState(outState); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (savedInstanceState != null) {//load your instance here 
     int position= savedInstanceState.getInt("InterestSpinner",0); 
     spinner.setSelection(interestSpiner); 
    } 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    int position= savedInstanceState.getInt("InterestSpinner",0); 
    spinner.setSelection(interestSpiner); 
} 
+0

我試過你的答案,它沒有奏效。當我的活動恢復時,它不會在恢復時進入onCreate()。那麼我應該如何在onResume方法中使用if(savedInstanceState)塊呢? –

+0

把你的活動的一些代碼,所以我可以確定你的問題! –

+0

我編輯了我的問題,並添加了一些塊,請參閱他們 –

相關問題