1

with this Login form, an admin with (ID: 001) should get a 'AdminActivity' whereas every other userID should get a 'UserActivity', and this is the code. It's not returning the adminID for some reason. Thank you

btnLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                UserFunctions userFunction = new UserFunctions();   

                String id = inputId.getText().toString();
                String pswd = inputPassword.getText().toString();

                JSONObject json = userFunction.loginUser(id, pswd);             

                try {
                    if (json.getString(KEY_SUCCESS) != null) {
                        loginErrorMsg.setText("");

                        String res = json.getString(KEY_SUCCESS); 

                        if(Integer.parseInt(res) == 1){

                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());

                            JSONObject json_user = json.getJSONObject("user");                          

                            if(id == "001"){
                                // Launch Admin Activity
                                Intent tab1 = new Intent(getApplicationContext(), AdminActivity.class);                     
                                    // Close all views before launching requestActivity
                                tab1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(tab1);
                            }

                            // Clear all previous data in database
                            userFunction.logoutUser(getApplicationContext());

                            db.addUser(json_user.getString(KEY_ID), 
                                       json_user.getString(KEY_NAME), 
                                       json_user.getString(KEY_EMAIL),                                     
                                       json_user.getString(KEY_CREATED_AT));                            

                                // Launch User Activity
                            Intent tab = new Intent(getApplicationContext(), UserActivity.class);                       
                                // Close all views before launching requestActivity
                            tab.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(tab);


                        }else{

                            loginErrorMsg.setText("Incorrect username/password");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });  
4

2 回答 2

2
    if("001".equals(id)){
                            // Launch Admin Activity
                            Intent tab1 = new Intent(getApplicationContext(), AdminActivity.class);                     
                                // Close all views before launching requestActivity
                            tab1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(tab1);
                            return;
                        }
于 2012-10-29T02:16:56.713 回答
1

You can't compare Strings in Java using ==
Try something like this:
if(id.equals("001"))

于 2012-10-29T02:16:27.093 回答