Java 如何删除TextChangedListener?

Java 如何删除TextChangedListener?,java,android,textwatcher,Java,Android,Textwatcher,我想使用EditText在数据库中搜索,然后将项目添加到RecylerView。现在我可以只搜索一个项目,项目显示,在更改编辑文本后,它将消失,我希望它保持不变,我希望搜索到的每个项目都自动添加到RecyclerView。 所以我想我可以删除ChangeTextListener,然后再添加它来搜索另一个项目,依此类推(?) 在MainActivity.kt中 fun Threads(){ editText.addTextChangedListener(对象:TextWatcher{ 在文本更改之

我想使用EditText在数据库中搜索,然后将项目添加到RecylerView。现在我可以只搜索一个项目,项目显示,在更改编辑文本后,它将消失,我希望它保持不变,我希望搜索到的每个项目都自动添加到RecyclerView。 所以我想我可以删除ChangeTextListener,然后再添加它来搜索另一个项目,依此类推(?)

在MainActivity.kt中

fun Threads(){
editText.addTextChangedListener(对象:TextWatcher{
在文本更改之前覆盖乐趣(
s:CharSequence,start:Int,
计数:Int,在:Int之后
) {
}
覆盖有趣的文本已更改(
s:CharSequence,start:Int,
在:Int之前,计数:Int
) {
线{
//我们在这里搜索项目
val itemsList=db?.costDAO()!!.getByName(s.toString())
runOnUiThread{
//这里我们将项传递给适配器构造函数
recyclerView.adapter=MyAdapter(this@MainActivity,项目列表)
adapter.notifyDataSetChanged()
}
}.start()
}
重写后文本已更改(s:可编辑){}
Cost.DAO

@Dao
接口CostDAO{
@查询(“从名称类似于:name的成本中选择*)
趣味getByName(名称:字符串):列表

要删除侦听器,必须将其设置为null;我知道您使用的是Kotlin,但在Java中是这样的:

editText.addTextChangedListener(null);

希望能有所帮助!

要删除侦听器,必须将其设置为null;我知道您正在使用Kotlin,但在Java中可能是这样的:

editText.addTextChangedListener(null);

希望有帮助!

罗伯特的回答不正确。这将导致崩溃

java.lang.NullPointerException:尝试在空对象引用上调用接口方法“void android.text.TextWatcher.beforeTextChanged(java.lang.CharSequence,int,int,int,int)”

您不能使用匿名类,因为您需要将其存储以删除

val textWatcher = object :TextWatcher {
                    override fun afterTextChanged(s: Editable?) {}

                    override fun beforeTextChanged(
                        s: CharSequence?,
                        start: Int,
                        count: Int,
                        after: Int
                    ) {}

                    override fun onTextChanged(
                        s: CharSequence?,
                        start: Int,
                        before: Int,
                        count: Int
                    ) {
                        //Do stuff
                    }

                }
binding.etEmail.addTextChangedWatcher(textWatcher)
// do what you need to do and later on
// ...
//
binding.etEmail.removeTextChangeWatcher(textWatcher)

罗伯特的回答不正确。这会导致撞车

java.lang.NullPointerException:尝试在空对象引用上调用接口方法“void android.text.TextWatcher.beforeTextChanged(java.lang.CharSequence,int,int,int,int)”

您不能使用匿名类,因为您需要将其存储以删除

val textWatcher = object :TextWatcher {
                    override fun afterTextChanged(s: Editable?) {}

                    override fun beforeTextChanged(
                        s: CharSequence?,
                        start: Int,
                        count: Int,
                        after: Int
                    ) {}

                    override fun onTextChanged(
                        s: CharSequence?,
                        start: Int,
                        before: Int,
                        count: Int
                    ) {
                        //Do stuff
                    }

                }
binding.etEmail.addTextChangedWatcher(textWatcher)
// do what you need to do and later on
// ...
//
binding.etEmail.removeTextChangeWatcher(textWatcher)