2013-07-24 59 views
0

在我的登錄活動中,我在選項菜單Login和Change User上創建了2個按鈕。如果用戶點擊更改用戶,則數據將從手機中刪除。我顯示一個對話框,要求用戶確認此操作。optionsmenu兩種情況都執行

它似乎工作正常,直到用戶註銷一次,然後不在用戶名和密碼textviews中輸入任何憑據。如果用戶點擊登錄按鈕時這些爲空,則顯示「請輸入有效名稱」的Toast消息,但對話框也會顯示詢問用戶是否想更改用戶。

我的問題是爲什麼對話框顯示要求用戶確認他們想要更改用戶,當此代碼不在選項菜單中的登錄情況下?這就像用戶點擊空字段登錄時那樣更改用戶代碼執行。預先感謝馬特。

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     Cursor allFromCompIdTable = nfcscannerapplication.loginValidate.queryAllFromCompanyIdTable(); 

     if(allFromCompIdTable.getCount() > 0){ 

      if(allFromCompIdTable.moveToLast()){ 

       compId = allFromCompIdTable.getString(allFromCompIdTable 
         .getColumnIndex(LoginValidate.C_COMPANY_ID_OUTSIDE_APP_PURPOSES)); 

      } 

     } 

     if(isAllowChangeUser.equalsIgnoreCase("false")){ 
      if(item.getItemId() == R.id.changeuser) 
      item.setVisible(false); 
      } 

     switch (item.getItemId()) { 


     case R.id.login: 




      Log.e(TAG, "login clicked from opts menu"); 



       theUsername = userName.getText().toString(); 

       if(theUsername.trim().equalsIgnoreCase("")){ 

        Toast.makeText(
          EntryActivity.this, 
          "Please enter a valid name.", 
          Toast.LENGTH_LONG).show(); 
        onCreate(sIs); 

       }else{ 

       thePassword = passwordPin.getText().toString(); 
       String loginType = "1"; 

       if(isSavePassword.trim().equalsIgnoreCase("false")){ 

        if(! thePassword.trim().equalsIgnoreCase("")){ 
       String[] params = new String[]{compId, theUsername, thePassword, loginType}; 

       //validate user Asynchonously on background thread 
       AsyncValidateCarer avc = new AsyncValidateCarer(); 
       avc.execute(params); 
        }else{ 
         Toast.makeText(
           EntryActivity.this, 
           "Please enter a valid password.", 
           Toast.LENGTH_LONG).show(); 
        } 
       }else if(isSavePassword.trim().equalsIgnoreCase("true")){ 

        Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer(); 
        String firstNameFromDB = null; 
        String passwordFromDB = null; 
        String carerIdFromDB = null; 

        if(c != null && c.getCount() > 0){ 

        c.moveToLast(); 
        firstNameFromDB = c.getString(c.getColumnIndex(LoginValidate.C_CARER_FIRSTNAME)); 
        passwordFromDB = c.getString(c.getColumnIndex(LoginValidate.C_PASSWORD)); 
        carerIdFromDB = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID)); 
        carerID = carerIdFromDB; 

        }else{ 
         String[] params = new String[]{compId, theUsername, thePassword, loginType}; 

         //validate user Asynchonously on background thread 
         AsyncValidateCarer avc = new AsyncValidateCarer(); 
         avc.execute(params); 
        } 

        if(theUsername.trim().equalsIgnoreCase(firstNameFromDB) && 
                thePassword.trim().equalsIgnoreCase(passwordFromDB)){ 

        Intent intent2 = new Intent(EntryActivity.this, 
          NfcscannerActivity.class); 
        intent2.setAction(CUSTOM_QRCODE_ACTION); 
        intent2.putExtra("carerid", carerID); 
        startActivity(intent2); 

        } 
       } 


      return true;      

       } 



     case R.id.changeuser: 


AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 

      alertDialogBuilder.setMessage("Are you sure you want to change user? All call history and messages will be deleted.") 
      .setCancelable(false) 
      .setPositiveButton("OK", 
      new DialogInterface.OnClickListener(){ 
      public void onClick(DialogInterface dialog, int id){ 

       Log.e(TAG, "change user button clicked"); 
       nfcscannerapplication.loginValidate.deleteTableCarer(); 
       nfcscannerapplication.loginValidate.deleteTableTransactions(); 
       Toast.makeText(
         EntryActivity.this, 
         "Carer logged out", 
         Toast.LENGTH_LONG).show(); 
       EntryActivity.this.onCreate(sIs); 

      } 
      }); 
      alertDialogBuilder.setNegativeButton("Cancel", 
      new DialogInterface.OnClickListener(){ 
      public void onClick(DialogInterface dialog, int id){ 



      } 
      }); 
      AlertDialog alert = alertDialogBuilder.create(); 
      alert.show(); 


      return true; 



     default: 

      return super.onOptionsItemSelected(item); 
     } 
    } 

回答

2

添加

break; 

在交換機的每個項目的結束。

+0

謝謝工作正常。我不得不添加一個返回true;在2個大小寫塊之外的方法結束處以及默認情況之後。 – turtleboy

0

在您的switch語句中發生了一次跌宕起伏。

一切都很好,直到theUsername.trim().equalsIgnoreCase("")計算爲true。執行進入if塊,繞過return語句,然後繼續case R.id.changeuser:

case R.id.login: 

     Log.e(TAG, "login clicked from opts menu"); 

      theUsername = userName.getText().toString(); 

      if(theUsername.trim().equalsIgnoreCase("")){ 
       Toast.makeText(
         EntryActivity.this, 
         "Please enter a valid name.", 
         Toast.LENGTH_LONG).show(); 
       onCreate(sIs); 

      }else{ 
相關問題