单击搜索按钮时无法获取 EditText 的文本

Can't get text of an EditText on search button click

我正在尝试在包含编辑文本和按钮的数组中进行搜索,但我无法从 EditText 中获取文本。例如,如果我将此 val textSearch : String = search.text.toString() 更改为 textSearch : String ="a",所有代码都有效,那么问题一定出在这里。我尝试了各种方法,但由于某种原因无法使代码正常工作。

class noticias : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        val view = inflater.inflate(R.layout.noticias, container, false) as View

        val listView : ListView = view?.findViewById(R.id.news_list_view)
        val btnSearch: Button = view?.findViewById(R.id.btnSearch)

        val search : EditText = view?.findViewById(R.id.searchText)
        val textSearch : String = search.text.toString()




        var names: ArrayList<NewsObject> = arrayListOf(
                NewsObject(R.drawable.bioron, "aaaa"),
                NewsObject(R.drawable.behind, "dddd"),
                NewsObject(R.drawable.bioron, "aaaa"),
                NewsObject(R.drawable.behind, "dddd"),
                NewsObject(R.drawable.bioron, "aaaa"),
                NewsObject(R.drawable.behind, "dddd"),
                NewsObject(R.drawable.juv, "jjjjkj")
        )

        listView.adapter = MyCustomAdapter(requireContext(),names)

        fun searchNews(search: String): ArrayList<NewsObject> {
            val namesTemp:ArrayList<NewsObject> =  arrayListOf()
            for(i in names.count()-1 downTo 0){
                Log.i("sda", names[i].title)
                if(names[i].title.contains(search.trim())){
                    namesTemp.add(names[i])
                }
            }

            return namesTemp


        }

        btnSearch.setOnClickListener() {


            if(textSearch.count() == 0){
                Toast.makeText(requireContext(),textSearch, Toast.LENGTH_LONG).show()
                listView.adapter = MyCustomAdapter(requireContext(),names)
            }else{
                Toast.makeText(requireContext(),"1", Toast.LENGTH_LONG).show()
                listView.adapter = MyCustomAdapter(requireContext(), searchNews(textSearch))
            }
        }




        return view

    }

    data class NewsObject(val img: Int, val title: String)


    public class MyCustomAdapter(context: Context, namesTemp: ArrayList<NewsObject>): BaseAdapter() {
        private val mContext: Context
        val names:ArrayList<NewsObject> =  namesTemp



        init {
            mContext = context
        }


        override fun getCount(): Int {
            return names.size
        }


        override fun getItemId(position: Int): Long {
            return position.toLong()
        }


        override fun getItem(position: Int): Any {
            return "TEST STRING"
        }


        override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
            val layoutInflater = LayoutInflater.from(mContext)
            val rowMain = layoutInflater.inflate(R.layout.news_row, viewGroup, false)

            val nameTextView = rowMain.findViewById<TextView>(R.id.name_textView)
            nameTextView.text = names.get(position).title

            /*val positionTextView = rowMain.findViewById<TextView>(R.id.position_textview)
            positionTextView.text = names.get(position).img*/

            val img = rowMain.findViewById<ImageView>(R.id.img_new)
            img.setImageResource(names.get(position).img)


            return rowMain

        }
    }
}

因为你在onCreateView中有这个:

val textSearch : String = search.text.toString()

您在 EditText 创建后立即从 EditText 检索文本,然后用户才有机会实际输入任何文本。因此,它始终是默认值:在本例中为空字符串。

您应该将该行移动到您的 OnClickListener 以便它仅在用户单击按钮时检索文本。