我尝试使用片段内的搜索视图搜索recyclelerview
我的适配器
public class AdapterActiveJob extends RecyclerView.Adapter<AdapterActiveJob.ViewHolderActiveJob> implements Filterable{
public ArrayList<JobsTest> listjobs = new ArrayList<>();
public ArrayList<JobsTest> filterlist;
public ArrayList<JobsTest> filteredjobs = new ArrayList<>();
private LayoutInflater layoutInflater;
Context ctx;
CustomFilter filter;
public AdapterActiveJob(Context context){
layoutInflater = layoutInflater.from(context);
//this.listjobs = listjobs;
//this.filterlist = listjobs;
}
public void setListjobs(ArrayList<JobsTest> listjobs, Context ctx){
this.listjobs = listjobs;
notifyItemRangeChanged(0,listjobs.size());
this.ctx = ctx;
this.filterlist = listjobs;
}
//Methods....
//Search attempt 1
public void filter(String constraint) {
if (constraint != null && constraint.length()>0){
//change to lowercase
constraint = constraint.toString().toLowerCase();
//Store filtered players here
// filteredjobs = new ArrayList<JobsTest>();
for(int i = 0;i<filterlist.size();i++){
//check
if(filterlist.get(i).getCustomer().toLowerCase().contains(constraint)){
//Add job
filteredjobs.add(filterlist.get(i));
}
}}
notifyDataSetChanged();
}
//Search attempt 2
@Override
public Filter getFilter() {
//Filtering logic
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
//change to lowercase
constraint = constraint.toString().toLowerCase();
//Store filtered players here
ArrayList<JobsTest> filteredlist = new ArrayList<>();
for (int i = 0; i < filterlist.size(); i++) {
//check
if (filterlist.get(i).getCustomer().toLowerCase().contains(constraint)) {
//Add job
filteredlist.add(filterlist.get(i));
}
}
results.count = filteredlist.size();
results.values = filteredlist;
} else {
results.count = filterlist.size();
results.values = filterlist;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
listjobs.addAll((ArrayList<JobsTest>)results.values);
notifyDataSetChanged();
}
};
}
}
我的片段
public class ActiveFragment extends Fragment implements SortListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//getActivity().invalidateOptionsMenu();
View view = inflater.inflate(R.layout.fragment_active, container, false);
textVolleyError = (TextView) view.findViewById(R.id.textVolleyError);
listJobs = (RecyclerView)view.findViewById(R.id.listJobs);
listJobs.setLayoutManager(new LinearLayoutManager(getActivity()));
adapterActiveJob = new AdapterActiveJob(getActivity());
//Add Item Decoration
listJobs.addItemDecoration(new DividerItemDecoration(getActivity()));
listJobs.setAdapter(adapterActiveJob);
if(savedInstanceState != null){
jobList = savedInstanceState.getParcelableArrayList(STATE_JOBS);
adapterActiveJob.setListjobs(jobList,getActivity());
}
else{sendJsonRequest();}
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.search);
SearchView sv = new SearchView(((Main2Activity) getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, sv);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
System.out.println("search query submit");
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
System.out.println("tap");
adapterActiveJob.filter(newText.toString());
return false;
}
});
}
当我输入除系统打印以外的搜索视图&#34;点按&#34;时,没有任何反应。帮助我成为一个白痴。