简体   繁体   English

SearchView 在 ArrayList Kotlin 中找不到搜索词

[英]SearchView could not find the searched word in ArrayList Kotlin

I created an ArrayList that holds data from the Firebase Realtime Database.我创建了一个 ArrayList,其中包含来自 Firebase 实时数据库的数据。 The problem is, the list checkbox data successfully appears with this then the ArrayList has been filled with data, but when using SearchView to search data it does not find anything (SearchView says ArrayList does not contain the data you are looking for), here is my code问题是,列表复选框数据成功出现,然后 ArrayList 已填充数据,但是当使用 SearchView 搜索数据时,它没有找到任何东西(SearchView 说 ArrayList 不包含您要查找的数据),这里是我的代码

databaseReference = FirebaseDatabase.getInstance().getReference("CF_DayaTarik")
    var dt: Data_DayaTarik2
    databaseReference!!.addValueEventListener(object : ValueEventListener {
        var dtList: ArrayList<Data_DayaTarik2?> = ArrayList<Data_DayaTarik2?>()
        override fun onDataChange(snapshot: DataSnapshot) {
            dtaList.clear()
            for (postSnapshot in snapshot.children) {
                val dt: Data_DayaTarik2? = postSnapshot.getValue(Data_DayaTarik2::class.java)
                dtList.add(dt)
            }
            Collections.sort(dtList) { s1, s2 -> String.valueOf(s1!!.nama_dayatarik).compareTo(s2!!.nama_dayatarik) }
            dataAdapter = MyCustomAdapter(this@PendukungKeputusan_DayaTarik, R.layout.list_dayatarik, dtList)
            val listView = findViewById<ListView>(R.id.list_dayatarik)
            listView.adapter = dataAdapter // set listview dengan dataadapter
           // dataAdapter!!.notifyDataSetChanged();
            searchview_spk.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
                override fun onQueryTextSubmit(query: kotlin.String?): Boolean {
                    //searchview_spk.clearFocus()
                    if(dtList.contains(query)){
                        dataAdapter!!.filter.filter(query)
                    }else{
                        Toast.makeText(applicationContext,"Maaf daya tarik yang anda cari belum ada",Toast.LENGTH_SHORT).show()
                    }
                    return false
                }
                override fun onQueryTextChange(newText: kotlin.String?): Boolean {
                    dataAdapter!!.filter.filter(newText)
                    return false
                }
            })
        }

        override fun onCancelled(error: DatabaseError) {}
    })
if(dtList.contains(query)){
    dataAdapter!!.filter.filter(query)
}else{
    Toast.makeText(applicationContext,"Maaf daya tarik yang anda cari belum ada",Toast.LENGTH_SHORT).show()
}

Here lies your problem, you are checking if这是您的问题,您正在检查是否

dtList.contains(query) dtList.contains(查询)

where, "dtList" contains items of type "Data_DayaTarik2" and you are trying to match that with "query" which is of type "String" .其中, "dtList"包含 " Data_DayaTarik2 " 类型的项目,您正试图将其与"String"类型的"query"匹配。

Hence, the condition is always false and it will never go inside if block and filter the list.因此,条件始终为假,并且如果阻止并过滤列表,它将永远不会在 go 内部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM