Firebase数据库搜索未返回任何结果

时间:2020-01-07 21:26:15

标签: android firebase kotlin firebase-realtime-database

我试图在使用Firebase作为后端的数据库中进行用户查找。我试图在使用Firebase作为后端的数据库中进行用户搜索。我想通过搜索视图进行搜索,以在recyclerview中显示结果。 但是,当进行搜索时,我的代码没有带来任何结果。

我的代码:

private fun configurarSearchView(view: View) {
        view.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String): Boolean {
                if (newText.isNotEmpty()) {
                    recyclerViewBeta.visibility = View.GONE
                    recyclerPesquisa.visibility = View.VISIBLE
                    pesquisarUsuarios(newText.toUpperCase())
                    Log.i("PESQUISA", newText.toUpperCase())
                } else {
                    recyclerViewBeta.visibility = View.VISIBLE
                    recyclerPesquisa.visibility = View.GONE
                }
                return true
            }
        })
    }

    private fun pesquisarUsuarios(texto: String) {
        Log.i("PESQUISA", "P: $texto")
        val query: Query = FirebaseDatabase.getInstance().getReference().child("users").orderByChild("nomePesquisa")
                .startAt(texto.toUpperCase()).endAt(texto.toUpperCase() + "\uf8ff")
        query.addListenerForSingleValueEvent(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (ds in dataSnapshot.children) { 
                    val usuario = ds.getValue(Usuario::class.java)!!
                    listaPesquisa.add(usuario)
                    Log.i("PESQUISA", "R: ${usuario.nomeExibicao}")
                }
                recyclerPesquisa.adapter?.let { it.notifyDataSetChanged() }
                val total: Int = listaPesquisa.size
                Log.i("PESQUISA", "total$total")
            }

            override fun onCancelled(databaseError: DatabaseError) {}
        })

数据库:

"usuarios" : {
    "5P6DzPIEuiSKU5UYGo03AwSMTED3" : {
      "email" : "valter@teste.com",
      "id" : "5P6DzPIEuiSKU5UYGo03AwSMTED3",
      "nickExibicao" : "valter",
      "nomeExibicao" : "Valter dos Santos",
      "nomePesquisa" : "VALTER DOS SANTOS",
      "status" : "1577010585880",
    },

为什么不显示数据?如何解决这个问题? 预先感谢!

1 个答案:

答案 0 :(得分:2)

您向数据库引用中提供了错误的子级用户,根据您的数据库结构,这些用户应该是用途。尝试使用

FirebaseDatabase.getInstance().getReference()
        .child("usuarios")
        .orderByChild("nomePesquisa")
        .startAt(texto.toUpperCase())
        .endAt(texto.toUpperCase() + "\uf8ff")
        .addListenerForSingleValueEvent();