我正在开发具有两种类型用户的移动应用程序。
在我的PHP代码中,我为每个用户分隔了布尔值。 success
(代表客户)和success1
(代表设计师)。
当我按下登录键时,错误提示首先是成功菜单配置文件的快速意图。
这是我来自LoginRegister.java
的代码行
private ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText userLoginUsername = (EditText) findViewById(R.id.loginUser);
final EditText userLoginPassword = (EditText) findViewById(R.id.loginPass);
final Button Login = (Button) findViewById(R.id.buttonLogin);
final Button Register = (Button) findViewById(R.id.buttonRegister);
loading = findViewById(R.id.loadinglogin);
//login
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = userLoginUsername.getText().toString();
final String password = userLoginPassword.getText().toString();
if(!username.isEmpty() && !password.isEmpty()) {
Login.setVisibility(View.GONE);
loading.setVisibility(View.VISIBLE);
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
boolean success1 = jsonResponse.getBoolean("success1");
//Client's Log in
if (success) {
//gikan sa php (green ones) to strings sa android
String username = jsonResponse.getString("username");
String name = jsonResponse.getString("name");
String number = jsonResponse.getString("number");
String gender = jsonResponse.getString("gender");
String address = jsonResponse.getString("address");
String occupation = jsonResponse.getString("occupation");
String birth_date = jsonResponse.getString("birth_date");
String user_type = jsonResponse.getString("user_type");
Intent intent = new Intent(LoginRegister.this, ProfileActivity.class);
//from strings to pass sa lain intents.
intent.putExtra("username",username);
intent.putExtra("number",number);
intent.putExtra("name", name);
intent.putExtra("gender", gender);
intent.putExtra("address", address);
intent.putExtra("occupation", occupation);
intent.putExtra("birthDate", birth_date);
intent.putExtra("userType", user_type);
LoginRegister.this.startActivity(intent);
finish();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginRegister.this);
builder.setMessage("Login Failed! Please provide valid username and password or connect to internet.")
.setNegativeButton("Retry", null)
.create()
.show();
Login.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
//Stylist's Log in
if(success1) {
String user_type = jsonResponse.getString("user_type");
Intent intent = new Intent(LoginRegister.this, ProfileActivity.class);
intent.putExtra("userType", user_type);
LoginRegister.this.startActivity(intent);
finish();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginRegister.this);
builder.setMessage("Login Failed! Please provide valid username and password or connect to internet.")
.setNegativeButton("Retry", null)
.create()
.show();
Login.setVisibility(View.VISIBLE);
loading.setVisibility(View.GONE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginRegister.this);
queue.add(loginRequest);
}else if(username.isEmpty() ){
userLoginUsername.setError("Please insert a username");
}else if(password.isEmpty()){
userLoginPassword.setError("Please put your password");
}
}
});
//register
Register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Register = new Intent(LoginRegister.this, RegisterCustomerOrStylist.class);
LoginRegister.this.startActivity(Register);
}
});
}
PS,它们具有来自不同表的不同数据。我所做的是,我有一个if条件,如果success
(客户端)的布尔值为true,它将传递数据,而其他则为alertdialog
用于错误登录。之后是success1
(设计师)的另一个if语句,它与客户端具有相同的逻辑。
答案 0 :(得分:2)
如果经过简化,您的代码如下所示。
//Client's Log in if (success) { } else { AlertDialog.Builder builder = ... } //Stylist's Log in if(success1) { } else { AlertDialog.Builder builder }
这意味着,如果设计师尝试登录,则会显示客户的“登录阻止警报”对话框,反之亦然。
因此,可能需要一个标志来检查是否存在成功。
boolean successAny = success || suucess1;
//Client's Log in
if (success) {
} else {
if (!successAny) {
AlertDialog.Builder builder = ...
}
}
...
NB。一个人既是客户,又不是设计师,不适合该样本。