首页 > 解决方案 > Android Studio 登录功能与数据库参考

问题描述

即使在遵循教程之后,似乎也无法破解我的头。请帮忙。

我应该改用firebase auth吗?还是我的数据库引用错误?当没有输入任何内容时,我能够启动应用程序并且错误工作正常。但是当输入用户名和密码时。什么都没发生。logcat 或活动中没有错误。

在此处输入图像描述

登录活动

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/loginpage"
    tools:context=".loginpage">


    <Button
        android:id="@+id/btnSignIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="34dp"
        android:text="SignIn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editPassword" />

    <Button
        android:id="@+id/btnRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="120dp"
        android:text="Register"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/editPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintStart_toStartOf="@+id/editUsername"
        app:layout_constraintTop_toBottomOf="@+id/editUsername" />

    <EditText
        android:id="@+id/editUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="59dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="62dp"
        android:layout_marginEnd="40dp"
        android:layout_marginRight="40dp"
        app:layout_constraintEnd_toEndOf="@+id/editUsername"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/oelogo" />

</androidx.constraintlayout.widget.ConstraintLayout>

登录 Java

    FirebaseDatabase database;


  DatabaseReference users;

    EditText editUsername, editPassword;
    Button btnSignIn, btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loginpage);

        database = FirebaseDatabase.getInstance();
        users = database.getReference("Login");

        editUsername = (EditText) findViewById(R.id.editUsername);
        editPassword = (EditText) findViewById(R.id.editPassword);

        btnSignIn = (Button) findViewById(R.id.btnSignIn);
        btnRegister = (Button) findViewById(R.id.btnRegister);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent s = new Intent(getApplicationContext(), staffregisterpage.class);
                startActivity(s);
            }
        });

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signIn(editUsername.getText().toString(),
                        editPassword.getText().toString());
            }
        });

    }

    private void signIn(final String username, final String password) {
        users.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.child(username).exists()) {
                    if (!username.isEmpty()) {
                        User login = dataSnapshot.child(username).getValue(User.class);
                        if (login.getPassword().equals(password)) {
                            Toast.makeText(loginpage.this, "Success Login", Toast.LENGTH_SHORT).show();
                            Intent home = new Intent(getApplicationContext(),homepage.class);
                            startActivity(home);
                        } else {
                            Toast.makeText(loginpage.this, "Wrong Password", Toast.LENGTH_SHORT).show();
                        }
                    } else
                        Toast.makeText(loginpage.this, "Username not registered", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

模型/用户 Java

private String username;
    private String password;


    public User(String username, String password){
        this.username = username;
        this.password = password;

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

标签: javaandroidfirebasefirebase-realtime-database

解决方案


用户名中的 A 是小写字母,而在数据库引用中它是大写字母。

在此处输入图像描述

此行不会返回 true。

if (dataSnapshot.child(username).exists())

要回答您的其他问题,是的 firebaseAuthentication 比这容易得多。


推荐阅读