英文:
How to make admin and user login using Firebase Realtime database
问题
expertFragment()显示除当前用户外的所有用户列表。我想要将用户分为两种类型-1. 专家 2. 普通用户。
向用户展示专家,向专家展示用户。
专家应通过其分配的ID和密码登录,而用户可以注册和登录。
专家账户应手动在Firebase实时数据库中创建,键为用户的'isExpert'。
我尝试过-
class ExpertFragment : Fragment(), iExpertAdapter {
private var _binding: FragmentExpertBinding? = null
private val binding get() = _binding!!
private lateinit var adapter: ExpertAdapter
private lateinit var expertList: ArrayList<User>
private lateinit var auth: FirebaseAuth
private lateinit var dbRef: DatabaseReference
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentExpertBinding.inflate(inflater, container, false)
val root: View = binding.root
auth = Firebase.auth
dbRef = FirebaseDatabase.getInstance().reference
expertList = ArrayList()
adapter = ExpertAdapter(this.requireContext(),expertList,this)
binding.expertRecylerView.adapter = adapter
dbRef.child("user").addValueEventListener(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
expertList.clear()
for(postSnapshot in snapshot.children){
val currentUser = postSnapshot.getValue(User::class.java)
if(auth.currentUser?.uid != currentUser?.uid && currentUser!!.isExpert){
expertList.add(currentUser!!)
}
}
adapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
}
})
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun onConsultBtnClicked(v:View, name: String?, uid: String) {
val bundle = bundleOf("name" to name,"uid" to uid)
v.findNavController().navigate(R.id.action_navigation_expert_to_chatFragment,bundle)
}
}
data class User(
val uid: String = "",
val name: String? = "",
val mail: String? = null,
val phoneNo: String? = null,
val address: String? = null,
val vehicleModleNo: String? = null,
val vehicleRegYear: String? = null,
val vehicleName: String? = null,
val rtoNo: String? = null,
val distanceTraveled: String? = null,
val isExpert: Boolean = false
)
但在if块中写上**&& currentUser!!.isExpert**会隐藏来自该片段的所有用户。
if(auth.currentUser?.uid != currentUser?.uid && currentUser!!.isExpert){
expertList.add(currentUser!!)
}
英文:
The expertFragment() which shows lists of all users (except the current user). What I want is to divide users in two types- 1. Expert 2. normal Users.
And to show experts to users and users to expert.
Expert should logIn and through their assigned id & password while the user can sign in and log in.
Experts account should be created mannuly in Firebase Realtime Database with the key in the user 'isExpert'.
What I tried-
class ExpertFragment : Fragment(), iExpertAdapter {
private var _binding: FragmentExpertBinding? = null
private val binding get() = _binding!!
private lateinit var adapter: ExpertAdapter
private lateinit var expertList: ArrayList<User>
private lateinit var auth: FirebaseAuth
private lateinit var dbRef: DatabaseReference
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentExpertBinding.inflate(inflater, container, false)
val root: View = binding.root
auth = Firebase.auth
dbRef = FirebaseDatabase.getInstance().reference
expertList = ArrayList()
adapter = ExpertAdapter(this.requireContext(),expertList,this)
binding.expertRecylerView.adapter = adapter
dbRef.child("user").addValueEventListener(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
expertList.clear()
for(postSnapshot in snapshot.children){
val currentUser = postSnapshot.getValue(User::class.java)
if(auth.currentUser?.uid != currentUser?.uid && currentUser!!.isExpert){
expertList.add(currentUser!!)
}
}
adapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
}
})
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun onConsultBtnClicked(v:View, name: String?, uid: String) {
val bundle = bundleOf("name" to name,"uid" to uid)
v.findNavController().navigate(R.id.action_navigation_expert_to_chatFragment,bundle)
}
}
data class User(
val uid: String = "",
val name: String? = "",
val mail: String? = null,
val phoneNo: String? = null,
val address: String? = null,
val vehicleModleNo: String? = null,
val vehicleRegYear: String? = null,
val vehicleName: String? = null,
val rtoNo: String? = null,
val distanceTraveled: String? = null,
val isExpert: Boolean = false
)
But writing this && currentUser!!.isExpert in the if block hides all the users from the fragment.
if(auth.currentUser?.uid != currentUser?.uid && currentUser!!.isExpert){
expertList.add(currentUser!!)
}
答案1
得分: 1
-
用双感叹号表示明确的值,当你想比较这个值时,也可以使用可空类型
而不是 currentUser!!.isExpert
使用 currentUser?.isExpert == true
这将检查当前用户是否可空,之后会检查expert是否为true值。 -
当从Firestore和实时数据库映射到数据模型时,布尔值不能直接读取。使用JVMField注解来读取它
@field:JvmField
val isExpert: Boolean = false
英文:
- Double bang is used for definite value when you want to compare the value you can use nullable as well
<br>instead of currentUser!!.isExpert
<br>use currentUser?.isExpert == true
this will check whether the current user is nullable or not. after this expert will be checked with true value.
- Boolean value is not read directly in the data model when mapped from firestore and realtime database. User JVMField annotation to read it
@field:JvmField
<br>val isExpert: Boolean = false
答案2
得分: 0
以下是您提供的代码的中文翻译部分:
dbRef.child("user").addValueEventListener(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
expertList.clear()
// 获取当前用户的 isExpert 状态。
val isExpert = snapshot.child(auth.currentUser!!.uid).child("isExpert").value
Log.e("isExpert", isExpert.toString())
for (postSnapshot in snapshot.children) {
val currentUser = postSnapshot.getValue(User::class.java)
// 如果当前用户是专家,则只有用户可见
if (isExpert == true) {
if (auth.currentUser?.uid != currentUser?.uid && currentUser?.isExpert == false) {
expertList.add(currentUser!!)
}
}
// 如果当前用户是普通用户,则只有专家可见
else {
if (auth.currentUser?.uid != currentUser?.uid && currentUser?.isExpert == true) {
expertList.add(currentUser!!)
}
}
}
adapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
}
})
请注意,我已将HTML转义字符(如"
和&
)还原为正常文本。
英文:
dbRef.child("user").addValueEventListener(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
expertList.clear()
// getting current user isExpert status.
val isExpert = snapshot.child(auth.currentUser!!.uid).child("isExpert").value
Log.e("isExpert",isExpert.toString())
for(postSnapshot in snapshot.children){
val currentUser = postSnapshot.getValue(User::class.java)
// if currentUser is an Expert then only users will be visible
if(isExpert == true) {
if (auth.currentUser?.uid != currentUser?.uid && currentUser?.isExpert == false) {
expertList.add(currentUser!!)
}
}
// if currentUser is a user then only experts will be visible
else{
if (auth.currentUser?.uid != currentUser?.uid && currentUser?.isExpert == true) {
expertList.add(currentUser!!)
}
}
}
adapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
}
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论