我有这个应用程序从MySql数据库中检索数据并将其解析为recyclerView。我有一个editText,我希望能够在键入时过滤recyclerView项目。
这是我的MainActivity(SearchAvtivity):
public class SearchActivity extends AppCompatActivity {
private static final String TAG = "Search Activity";
private Context mContext = SearchActivity.this;
private static final int ACTIVITY_NUM = 1;
String urlAddress = "myDatabaseAddressGoesHere.PHP";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Log.d(TAG, "onCreate: started");
setupBottomNavigationView();
final RecyclerView rv = (RecyclerView) findViewById(R.id.SearchRv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setItemAnimator(new DefaultItemAnimator());
Downloader dl = new Downloader(SearchActivity.this,urlAddress,rv);
dl.execute();
}
/**
* Setup Bottom Navigation View
*/
private void setupBottomNavigationView(){
Log.d(TAG, "setupBottomNavigationView: Setting up Bottom Navigation View");
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx)findViewById(R.id.bottom_nav_bar);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
BottomNavigationViewHelper.enableNavigation(mContext,bottomNavigationViewEx);
Menu menu = bottomNavigationViewEx.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
}
这是MyAdapter:
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
Context c;
ArrayList<SpaceCraft> spaceCrafts;
List<MyHolder> displayedList;
public MyAdapter(Context c, ArrayList<SpaceCraft> spaceCrafts) {
this.c = c;
this.spaceCrafts = spaceCrafts;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_adapter_vertical,parent,false);
return new MyHolder(v);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.nameTv.setText(spaceCrafts.get(position).getName());
holder.addressTv.setText(spaceCrafts.get(position).getAddress());
holder.phoneTv.setText(spaceCrafts.get(position).getPhone());
holder.emailTv.setText(spaceCrafts.get(position).getEmail());
}
@Override
public int getItemCount() {
return spaceCrafts.size();
}
}
我不知道你是否需要查看vlasses:下载程序,解析器,连接器或持有者。我请你告诉我。
答案 0 :(得分:1)
你应该使用TextWatcher()For RecyclerView。
答案 1 :(得分:-1)
你可以在editText上使用TextWatcher,在里面过滤你的列表。
示例:
((EditText) findViewById(R.id.your_edit_text)).addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
String text = s.toString();
ArrayList<String> yourCopyList = new ArrayList<>(yourList)
yourList.clear();
if (text.isEmpty()) {
addAllYourDataToList();
} else {
text = text.toLowerCase();
for (int i = 0; i<yourCopyList.size(); i++) {
// Adapt the if for your usage
if (yourCopyList.get(i).getName().toLowerCase().contains(text)) {
yourList.add("the string");
}
}
}
adapter.notifyDataSetChanged();
}
});
在此示例中,您将删除列表中的所有数据,并仅添加文本过滤器中对应的数据