我如何使用 Kotlin 在 Android 上的 Firebase 中使用基于 Spinner 值的约束从同一登录页面登录管理员、学生和讲师

问题描述

我正在制作这个应用程序,它可以让管理员、学生、讲师和家长从一个登录页面登录,使用 Firebase 实时数据库,以及您选择想要登录用户的微调器。

Login page of Multiuser app

Login page of the same Multiuser app

管理员注册其他用户并选择对应于任何用户的微调器值。

Register User

所以我希望这些用户登录到他们自己的活动,只要登录中的微调器值与管理员创建的 Firebase 中保存的微调器值相匹配。 下面是我的 firebase 数据库

Firebase Schema

下面是登录页面代码,当“用户”的 Spinner 值与管理员注册用户时发送到数据库的 Spinner 值匹配时,我希望用户根据他们从 Spinner 中的选择登录到不同的活动.

package com.example.studentclassprogresstracker
import android.app.ProgressDialog
import android.content.ContentValues.TAG
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.*
import kotlinx.android.synthetic.main.activity_login.*


class Login : AppCompatActivity() {
    private lateinit var database: DatabaseReference

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)




        loginbtn.setonClickListener {
            loginUser()
//movetousers()
        }
    }

    private fun movetousers() {
        val spin: String = userchioce.selectedItem.toString()


        if (spin.equals("Student")) {
            val intent = Intent(applicationContext,Student::class.java)
            startActivity(intent)
        }
        if (spin.equals("Lecturer")) {
            val intent = Intent(applicationContext,Lecturer::class.java)
            startActivity(intent)
        }
        if (spin.equals("Parent")) {
            val intent = Intent(applicationContext,Parent::class.java)
            startActivity(intent)
        }


    }


    fun loginUser() {
        val Password: String = loginpassword?.text.toString()
        val Email: String = loginEmail?.text.toString()


        when {
            TextUtils.isEmpty(Email) -> Toast.makeText(
                this,"Enter Email or Username",Toast.LENGTH_LONG
            ).show()
            TextUtils.isEmpty(Password) -> Toast.makeText(this,"Enter Password",Toast.LENGTH_LONG)
                .show()


            else -> {
                val progressDialog = ProgressDialog(this)
                progressDialog.setTitle("Login")
                progressDialog.setMessage("Authenticating...")
                progressDialog.setCanceledOnTouchOutside(false)
                progressDialog.show()
                val mAuth: FirebaseAuth = FirebaseAuth.getInstance()

                mAuth.signInWithEmailAndPassword(Email,Password).addOnCompleteListener { task ->


                    val spin: String = userchioce.selectedItem.toString()

                    if (spin.equals("Admin")) {
                        if (task.isSuccessful) {
                            progressDialog.dismiss()
                            Toast.makeText(this,"Login Successful",Toast.LENGTH_LONG).show()
                            val intent = Intent(applicationContext,Admin::class.java)
                            startActivity(intent)
                        } else {

                            val message = task.exception!!.toString()
                            Toast.makeText(this,"not registered $message",Toast.LENGTH_LONG)
                                .show()
                            FirebaseAuth.getInstance().signOut()
                            progressDialog.dismiss()
                        }
                    }
                    val uid = FirebaseAuth.getInstance().currentUser!!.uid
                    val rootRef = FirebaseDatabase.getInstance().reference
                    val uidRef = rootRef.child("Users").child("username")
                    val valueEventListener = object : ValueEventListener {
                        override fun onDataChange(snapshot: DataSnapshot) {
                            val user = snapshot.getValue(User::class.java)
                            Log.d(TAG,"user" + user?.users)
                        }

                        override fun onCancelled(error: DatabaseError) {
                            Todo("Not yet implemented")
                        }

                    }
                    uidRef.addListenerForSingleValueEvent(valueEventListener)

                    val query = FirebaseDatabase.getInstance().getReference("Users").child(uid)
                        .orderByChild("users").equalTo("Student")
                    val ii = query.addListenerForSingleValueEvent(valueEventListener)

                    if (spin.equals("Student") && ii.toString().equals("Student")) {
//                            spin.equals("Student") &&
                        if (task.isSuccessful) {
                            progressDialog.dismiss()
                            Toast.makeText(this,Student::class.java)
                            startActivity(intent)
                        } else {

                            val message = task.exception!!.toString()

                            Toast.makeText(this,Toast.LENGTH_LONG)
                                .show()
                            FirebaseAuth.getInstance().signOut()
                            progressDialog.dismiss()

                        }
                    }
                    if (spin.equals("Lecturer")) {
                        if (task.isSuccessful) {
                            progressDialog.dismiss()
                            Toast.makeText(this,Lecturer::class.java)
                            startActivity(intent)
                        } else {

                            val message = task.exception!!.toString()
                            Toast.makeText(this,Toast.LENGTH_LONG)
                                .show()
                            FirebaseAuth.getInstance().signOut()
                            progressDialog.dismiss()
                        }
                    }
                    if (spin.equals("Parent")) {
                        if (task.isSuccessful) {
                            progressDialog.dismiss()
                            Toast.makeText(this,Parent::class.java)
                            startActivity(intent)
                        } else {

                            val message = task.exception!!.toString()
                            Toast.makeText(this,Toast.LENGTH_LONG)
                                .show()
                            FirebaseAuth.getInstance().signOut()
                            progressDialog.dismiss()
                        }
                    }


                }
            }


        }


    }
}

以下是管理员注册活动的代码

package com.example.studentclassprogresstracker
import android.app.ProgressDialog
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.activity_admin_login_users.*

class AdminLoginUsers : AppCompatActivity() {
    private lateinit var database: DatabaseReference
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_admin_login_users)
        registerBtn.setonClickListener {
            createAccount()

        }
    }


    private fun createAccount() {
        val password = regpassword.text.toString()
        val username = regusername.text.toString()
        val email = emailreg.text.toString()
        val lastname = lastName.text.toString()
        val firstname = firstName.text.toString()
        val spin: String = userchoicereg.selectedItem.toString()
        val users = spin


        when {
            TextUtils.isEmpty(password) -> Toast.makeText(
                this,"password is required.",Toast.LENGTH_LONG
            ).show()
            TextUtils.isEmpty(username) -> Toast.makeText(
                this,"Username is required.",Toast.LENGTH_LONG
            ).show()
            TextUtils.isEmpty(email) -> Toast.makeText(
                this,"email is required.",Toast.LENGTH_LONG
            ).show()
            TextUtils.isEmpty(lastname) -> Toast.makeText(
                this,"surname is required.",Toast.LENGTH_LONG
            ).show()
            TextUtils.isEmpty(firstname) -> Toast.makeText(
                this,"firstname is required.",Toast.LENGTH_LONG
            ).show()
            TextUtils.isEmpty(users) -> Toast.makeText(
                this,"User Type is required.",Toast.LENGTH_LONG
            ).show()

            else -> {

                val progressDialog = ProgressDialog(this)
                progressDialog.setTitle("Registering")
                progressDialog.setMessage("wait for registration to complete")
                progressDialog.setCanceledOnTouchOutside(false)
                progressDialog.show()

                val mAuth: FirebaseAuth = FirebaseAuth.getInstance()

                mAuth.createuserWithEmailAndPassword(email,password)
                    .addOnCompleteListener { task ->
                        if (task.isSuccessful) {

                            saveuserinfo()
                            progressDialog.dismiss()
                        } else {
                            val message = task.exception!!.toString()
                            Toast.makeText(this,Toast.LENGTH_LONG)
                                .show()
                            mAuth.signOut()
                            progressDialog.dismiss()
                        }

                    }
            }
        }


    }

    fun saveuserinfo() {

        val Users = userchoicereg.selectedItem.toString()
        val Firstname = firstName.text.toString()
        val Lastname = lastName.text.toString()
        val Username = regusername.text.toString()
        val Email = emailreg.text.toString()

        val progressDialog = ProgressDialog(this)
        //val users = spin


        database = FirebaseDatabase.getInstance().getReference("Users")
        val user = User(Firstname,Lastname,Username,Email,Users)

        database.child(Username).setValue(user).addOnCompleteListener { task ->

            if (task.isSuccessful) {
                firstName.text.clear()
                lastName.text.clear()
                regusername.text.clear()
                regpassword.text.clear()
                emailreg.text.clear()
                progressDialog.dismiss()
                Toast.makeText(this,"Account has been created",Toast.LENGTH_LONG).show()


            } else {
                val message = task.exception!!.toString()
                Toast.makeText(this,Toast.LENGTH_LONG).show()
                FirebaseAuth.getInstance().signOut()
                progressDialog.dismiss()
            }
        }
    }

}

下面是我的数据类“用户

    package com.example.studentclassprogresstracker

data class User(val firstname: String? = null,val lastname: String? = null,val username: String? = null,val email: String? = null,val users: String? = null )

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)