我的 RecyclerView
和 EditText
用于搜索填充了 Firebase 数据库数据的匹配项。当我自己在 EditText
中输入一些单词时,一切正常,但如果我尝试 intent
单词从另一个 Activity 搜索到此 EditText
,没有任何效果。为了让搜索工作之后,我需要更改目标词中的一些字母。
这是我在 OnCreate 中的代码:
if(getIntent().getExtras() != null) {
Intent intent = getIntent();
String str = intent.getStringExtra("trickname");
editSearch.setText(str);
}
editSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
search(s.toString());
}
});
及搜索方法:
private void search(String str){
ArrayList<Trick> myList = new ArrayList<>();
for(Trick object : list){
if((object.getDescription().toLowerCase().contains(str.toLowerCase().trim())) || (object.getName().toLowerCase().contains(str.toLowerCase().trim()))){
myList.add(object);
}
}
AdapterClass adapterClass = new AdapterClass(myList);
recyclerView.setAdapter(adapterClass);
}
来自其他 Activity 的 Intent :
Intent intent1 = new Intent(ProgressActivity.this, EncyActivity.class);
intent1.putExtra("trickname", button1.getText().toString());
startActivity(intent1);
EditText
中的单词是有意的,但搜索不起作用。如果我自己输入这个词,搜索就会正常工作并找到匹配项
最佳答案
只需调用 search(str);
之后editSearch.setText(str);
关于java - 在将搜索字符串指定到我的 EditText 后,如何开始在回收器 View 中搜索匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56188992/