سؤال

In my login activity i've created 2 button s on the options menu, Login and Change User. If the user clicks change user then data is deleted from the phone. I display a dialog box asking the user to confirm this action.

It seems to work fine until the user logs out once then doesn't enter any credentials in the username and password textviews. If these are empty when the user clicks the login button then a toast message displays showing 'Please enter a valid name' but the dialog box also displays asking if the user wants to change user.

my question is why does the dialog box display that asks the user to confirm they want to change user when this code is not in the login case within the options menu? It is like when the user clicks login with empty fields then change user code executes. Thanks in advance matt.

@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);
        }
    }
هل كانت مفيدة؟

المحلول

Add a

break;

At the end of each item of your switch.

نصائح أخرى

A fall-through is happening in your switch statement cases.

Everything is fine until theUsername.trim().equalsIgnoreCase("") computes to true. The execution enters the if-block, bypasses the return statement, and goes on with 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{
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top