在一个带有三个选项卡且在工具栏上具有搜索功能的应用程序上工作,然后从SQLite提取数据并填充回收者视图中的数据,我也尝试向其添加搜索过滤器,但它在下面不起作用是我的代码。任何帮助将不胜感激下面是我的适配器代码和TabFragment过滤器
private ArrayList<books> book = new ArrayList<>();
private List<books> contactListFiltered;
List<View>itemViewList = new ArrayList<>();
private List<books> contactList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, booker, time;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
booker = (TextView) view.findViewById(R.id.body);
time = (TextView) view.findViewById(R.id.time);
}
}
public MoviesAdapter(List<books> book) {
this.book = (ArrayList<books>) book;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.books, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
books movie = book.get(position);
holder.title.setText(movie.getTitle());
holder.booker.setText(movie.getBody());
holder.time.setText(formatDate(books.getTimestamp()));
}
@Override
public int getItemCount() {
if (book == null)
return 0;
else
return book.size();
}
private String formatDate(String dateStr) {
try {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = fmt.parse(dateStr);
SimpleDateFormat fmtOut = new SimpleDateFormat("MMM dd");
return fmtOut.format(date);
} catch (ParseException e) {
}
return "";
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
contactListFiltered = contactList;
} else {
List<books> filteredList = new ArrayList<>();
for (books row : contactList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getBody().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
contactListFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = contactListFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
contactListFiltered = (ArrayList<books>) filterResults.values;
notifyDataSetChanged();
}
};
}
这是我的Tab片段代码:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
if (searchManager != null) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getActivity().getComponentName()));
}
searchView.setMaxWidth(Integer.MAX_VALUE);
// listening to search query text change
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// filter recycler view when query submitted
mAdapter.getFilter().filter(query);
recyclerView.setAdapter(mAdapter);
Toast.makeText(getContext(),"the search is on",Toast.LENGTH_LONG).show();
/// tell the recycler there is a different data to display
mAdapter.notifyDataSetChanged();
return false;
}
@Override
public boolean onQueryTextChange(String query) {
// filter recycler view when text is changed
mAdapter.getFilter().filter(query);
recyclerView.setAdapter(mAdapter);
/// tell the recycler there is a different data to display
mAdapter.notifyDataSetChanged();
return false;
}
});
}