【问题标题】:Firebase Database Search Query not working properlyFirebase 数据库搜索查询无法正常工作
【发布时间】:2020-06-15 09:45:37
【问题描述】:

我正在创建一个社交应用程序,例如 facebook,但在使用 Firebase 数据库查询进行查找朋友活动时遇到了问题。

问题是搜索结果(用户)没有显示。 当我将 allUsersRef 作为参数放入 setQuery() 时,会显示用户,但是当我将 searchFriendsQuery 作为参数时,它不会显示任何内容。 我找了很多答案来解决我的问题,但没有一个有效。

这是我卡住的方法,正如我所说,该方法通过全名搜索 firebase 数据库,然后显示与用户输入匹配的用户:

private fun searchFriends(userInputText: String) {
        Log.i("SEARCH",userInputText)
        Toast.makeText(this,"Searching...",Toast.LENGTH_LONG).show()

        val searchFriendsQuery = allUsersRef.orderByChild("fullname")
            .startAt(userInputText)
            .endAt(userInputText + "\uf8ff")

        val options: FirebaseRecyclerOptions<FindFriends> = FirebaseRecyclerOptions.Builder<FindFriends>()
            .setQuery(searchFriendsQuery,FindFriends::class.java)
            .build()

        val firebaseRecyclerAdapter = object: FirebaseRecyclerAdapter<FindFriends,FindFriendsViewHolder>(options) {
            override fun onBindViewHolder(viewHolder: FindFriendsViewHolder, position: Int, model: FindFriends) {
                viewHolder.setProfilesInfo(model)
            }
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FindFriendsViewHolder {
                val view: View = LayoutInflater.from(parent.context)
                    .inflate(R.layout.users_display_layout, parent, false)
                return FindFriendsViewHolder(view)
            }
        }
        firebaseRecyclerAdapter.startListening()
        searchOutputList.adapter = firebaseRecyclerAdapter
    }

这是我的 viewHolder 类:

class FindFriendsViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        var mView = itemView

        private val friendsProfileImage: CircleImageView = mView.findViewById(R.id.find_users_profile_image)
        private val friendsFullName: TextView = mView.findViewById(R.id.find_user_full_name)
        private val friendsStatus: TextView = mView.findViewById(R.id.find_user_status)

        fun setProfilesInfo(friends: FindFriends){
            friendsFullName.text = friends.fullname
            friendsStatus.text = friends.status
            Glide.with(itemView.context)
                .load(friends.profileimage)
                .into(friendsProfileImage)
        }
    }

这是我的模块类:

class FindFriends {
    var profileimage: String? = null
    var fullname: String? = null
    var status: String? = null
}

这些是代码中变量的值:

private lateinit var allUsersRef: DatabaseReference
private lateinit var searchOutputList: RecyclerView
private lateinit var searchInputText: EditText

allUsersRef = FirebaseDatabase.getInstance().reference.child("Users")

searchOutputList = findViewById(R.id.search_list)
searchOutputList.setHasFixedSize(true)
searchOutputList.layoutManager = LinearLayoutManager(this)

searchInputText = findViewById(R.id.search_input_box).toString()

JSON 导出文件:

"Users" : {
    "Q4RWEkf8YsRnQppwpEYrSMKa0M82" : {
      "country" : "Mexico",
      "dateofbirth" : "Unknown",
      "fullname" : "Oswaldo Escobedo",
      "gender" : "Unknown",
      "profileimage" : "https://firebasestorage.googleapis.com/v0/b/myhero-8778b.appspot.com/o/Profile%20Images%2FQ4RWEkf8YsRnQppwpEYrSMKa0M82.jpg?alt=media&token=44fe278b-a7f0-42c7-90b9-dfbcc57bf733",
      "relationshipstatus" : "Unknown",
      "status" : "Active",
      "username" : "oswi73"
    },
    "h7DPxSQax3PNYFcXNDSgk4H9zyF2" : {
      "country" : "Mexico",
      "dateofbirth" : "Unknown",
      "fullname" : "Jenny Balandran",
      "gender" : "Unknown",
      "profileimage" : "https://firebasestorage.googleapis.com/v0/b/myhero-8778b.appspot.com/o/Profile%20Images%2Fh7DPxSQax3PNYFcXNDSgk4H9zyF2.jpg?alt=media&token=99633998-46f4-4a64-806d-3a8bc7ec0580",
      "relationshipstatus" : "Unknown",
      "status" : "Active",
      "username" : "oescob16"
    },
    "nq6OPYGHuAdCApuyXhLYHLLdOtA3" : {
      "country" : "México ",
      "dateofbirth" : "Unknown",
      "fullname" : "Oswaldo Escobedo",
      "gender" : "Unknown",
      "profileimage" : "https://firebasestorage.googleapis.com/v0/b/myhero-8778b.appspot.com/o/Profile%20Images%2Fnq6OPYGHuAdCApuyXhLYHLLdOtA3.jpg?alt=media&token=55f8c342-9e74-400f-8038-5a7af208510e",
      "relationshipstatus" : "Unknown",
      "status" : "Active",
      "username" : "oescobedo3 "
    }
  }

这是userInputText的日志值:

2020-06-14 19:26:11.288 10129-10129/com.example.bemyhero I/SEARCH: androidx.appcompat.widget.AppCompatEditText{e57e7ff VFED..CL. .F...... 30,103-1080,313 #7f08012a app:id/search_input_box aid=1073741824}

【问题讨论】:

  • 请将您的问题编辑为:1) 记录userInputText 的值,然后包含更新后的代码及其输出,2) 在您的数据库中allUsersRef 的 JSON(作为文本,没有屏幕截图)。您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。
  • @FrankvanPuffelen 我已经添加了 JSON 文件。但我不知道热记录userInputText的值。你能帮帮我吗?
  • 只需在searchFriends 的顶部执行Log.i("SEARCH", userInputText) 之类的操作。
  • @FrankvanPuffelen 我已经添加了日志,现在该怎么办?

标签: java android firebase kotlin firebase-realtime-database


【解决方案1】:

您似乎在EditText 字段上调用了toString。将其传递给数据库将不起作用,因为您将在数据库中搜索文字字符串 "androidx.appcompat.widget.AppCompatEditText{e57e7ff VFED..CL. .F...... 30,103-1080,313 #7f08012a app:id/search_input_box aid=1073741824}"

您需要从字段中获取文本,然后搜索:

searchFriends(textField.getText().toString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-12
    • 2016-09-07
    • 2017-04-25
    • 2017-12-23
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多