好的,我正在制作一个在线捐赠门户android应用程序。该应用程序将有2个主要用户:
根据用户类型有不同的UI。
下面是注册表格:
下面是登录表单:
然后将表单中的数据添加到数据库中,如下所示(注册后):
以下是注册课程:
package com.example.android.edonate;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class SignUp extends AppCompatActivity {
//variables to store data from the form
private EditText fname;
private EditText lname;
private EditText email;
private EditText cellNo;
private EditText password;
private Spinner type;
private Button signUp;
//Firebase instance variables
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mUsersDatabaseReference;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
//Initialize firebase components:
mFirebaseDatabase = FirebaseDatabase.getInstance();
//to set to specific location within the database and storage
mUsersDatabaseReference = mFirebaseDatabase.getReference().child("users");
//Initialize firebase authentication
mAuth = FirebaseAuth.getInstance();
//to get data from the form:
fname=(EditText)findViewById(R.id.fname);
lname=(EditText)findViewById(R.id.lname);
email=(EditText)findViewById(R.id.email);
cellNo=(EditText)findViewById(R.id.mobile_number);
password=(EditText)findViewById(R.id.password);
type=(Spinner)findViewById(R.id.user_category);
signUp = (Button)findViewById(R.id.button_sign_up_2);
//Send button sends a message and clears the EditText
signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
User user = new User(fname.getText().toString(), lname.getText().toString(), email.getText().toString(), cellNo.getText().toString(), password.getText().toString(), type.getSelectedItem().toString());
mUsersDatabaseReference.push().setValue(user); //add user details to the database
RegisterUser();
//return to home page after signing up:
//Intent intent = new Intent(SignUp.this,MainActivity.class);
//startActivity(intent);
}
});
}
//to register user to the Firebase 'users'
public void RegisterUser(){
String Email = email.getText().toString().trim();
String Password = password.getText().toString().trim();
if (TextUtils.isEmpty(Email)){
Toast.makeText(this, "Email field is Empty!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(Password)){
Toast.makeText(this, "Password field is Empty!", Toast.LENGTH_SHORT).show();
return;
}
mAuth.createUserWithEmailAndPassword(Email, Password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
//check if successful
if (task.isSuccessful()) {
//User is successfully registered and logged in
//start Profile Activity here
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}else{
Toast.makeText(SignUp.this, "Couldn't register, try again",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}
}
根据登录用户的“类型”(如数据库中所示)是捐赠者还是慈善机构,我想做的是打开一个不同的活动。因此,在“登录”类中需要执行以下步骤: 1.验证用户电子邮件和密码(我已经执行过) 2.从具有该特定电子邮件的数据库中获取用户记录。 3.检查所选用户记录的“类型”字段是“慈善”还是“捐赠者” 4.根据类型字段的值打开相关活动
第1步和第4步将很容易实现。我需要帮助的第2步和第3步。步骤2和3将在SigIn.java类中实现。现在,我默认情况下会打开DonorHome.java cals,但是我需要添加一个条件来打开Donor.Home和CHarity.Home
下面是SignIn.java类的代码。我需要在这里添加什么相关代码?
package com.example.android.edonate;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.util.Log;
import android.net.Uri;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.firebase.ui.auth.AuthUI;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseError;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.Arrays;
import java.util.List;
public class SignIn extends AppCompatActivity {
private EditText email;
private EditText password;
private FirebaseAuth mAuth;
private FirebaseUser currentUser;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mUsersDatabaseReference;
private Button signIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
email = (EditText)findViewById(R.id.input_email);
password = (EditText)findViewById(R.id.input_password);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
mFirebaseDatabase = FirebaseDatabase.getInstance();
mUsersDatabaseReference = mFirebaseDatabase.getReference();
//.child("users");
signIn = (Button)findViewById(R.id.button_sign_in_2);
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == signIn){
LoginUser();
}
}
});
}
public void LoginUser(){
final String Email = email.getText().toString().trim();
String Password = password.getText().toString().trim();
mAuth.signInWithEmailAndPassword(Email, Password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
finish();
startActivity(new Intent(getApplicationContext(),
DonorHome.class));
}else {
Toast.makeText(SignIn.this, "couldn't login",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
答案 0 :(得分:0)
首先告诉您,StackOverflow并非源于解决方案代码请求,而是源于问题或疑问,因此要求很多代码行并不那么合适。
无论如何,您需要执行以下操作:
public void LoginUser(){
final String Email = email.getText().toString().trim();
String Password = password.getText().toString().trim();
mAuth.signInWithEmailAndPassword(Email, Password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
getUserInformation();
//finish(); Don't finish activity now
//startActivity(new Intent(getApplicationContext(),
// DonorHome.class)); Don't start activity without know user type
}else {
Toast.makeText(SignIn.this, "couldn't login",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void getUserInformation(){
//here get User information by firebase querying and get result.....
...onSuccess(){
if(userType.equals("Donor")
startDonorActivityMain();
else
startCharityActivityMain();
}
}