I am trying to implement a search filter for my app but I don't know how to do it.I saw a question this one here but it only made me more confused.This is what I have done this far:
I call the Fragment Class in my main activity like this
private JobsFragment jobsFragment=new JobsFragment();
Main-Class Activity
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final SearchView searchView=(SearchView) menu.findItem(R.id.action_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
Query querySearch = fb.collection("jobs").whereEqualTo("title", text);
if (!text.trim().isEmpty()){
jobsFragment.jobView(querySearch);
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.trim().isEmpty()){
}
return false;
}
});
return true;
}
The RecycleView is in JobsFragment :
public void jobView(Query query){
recycle.setItemAnimator(new DefaultItemAnimator());
FirestoreRecyclerOptions<JobsLists> options = new FirestoreRecyclerOptions.Builder<JobsLists>()
.setQuery(query, JobsLists.class)
.build();
adapter = new FirestoreRecyclerAdapter<JobsLists, JobsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final JobsViewHolder holder, final int position, @NonNull final JobsLists jobsmodel) {
holder.setTitle(jobsmodel.getTitle());
holder.setDate(jobsmodel.getDate());
holder.setCo_Name(jobsmodel.getCo_Name());
holder.setSkills(jobsmodel.getSkills());
holder.setLocation(jobsmodel.getLocation());
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, JobView.class);
intent.putExtra("title", jobsmodel.getTitle());
intent.putExtra("skills", jobsmodel.getSkills());
intent.putExtra("company", jobsmodel.getCo_Name());
intent.putExtra("description", jobsmodel.getDescription());
context.startActivity(intent);
}
});
}
@NonNull
@Override
public JobsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_card, parent, false);
return new JobsViewHolder(view);
}
@Override
public int getItemViewType(int position) {
return position;
}
};
recycle.setAdapter(adapter);
}
Full StackTrace
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setItemAnimator
(android.support.v7.widget.RecyclerView$ItemAnimator)' on a null object reference
at com.example.dell.ora.JobsFragment.jobView(JobsFragment.java:57)
at com.example.dell.ora.Maino$2.onQueryTextSubmit(Maino.java:71)
at android.support.v7.widget.SearchView.onSubmitQuery(SearchView.java:1189)
at android.support.v7.widget.SearchView$7.onEditorAction(SearchView.java:1166)
at android.widget.TextView.onEditorAction(TextView.java:5207)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…