如何捕获给定的用户ID以在登录中显示不同的活动?

时间:2012-10-29 02:10:40

标签: java android

使用此登录表单,管理员(ID:001)应该获得'AdminActivity',而其他每个userID都应该获得'UserActivity',这就是代码。由于某种原因,它没有返回adminID。谢谢

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();
                }
            }
        });  

2 个答案:

答案 0 :(得分: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;
                        }

答案 1 :(得分:1)

您无法使用==来比较Java中的字符串 尝试这样的事情:
if(id.equals("001"))