我正在实施一个 earchview 来过滤来自 Firebase 的数据。加载此活动时,页面已加载,我可以看到卡片,但是当我点击搜索栏时,活动变为空,因为回收器视图为空。如果我从搜索功能中删除"recyclerView.setAdapter(firebaseRecyclerAdapter(;",数据再次可见,但过滤器不起作用。
创建
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
recyclerView=(RecyclerView)findViewById(R.id.myRecycleView);
recyclerView.setHasFixedSize(true);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Reference");
mDatabase.keepSynced(true);
options= new FirebaseRecyclerOptions.Builder<New>().setQuery(mDatabase, New.class).build();
firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<New, ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ViewHolder viewHolder, int i, @NonNull New model) {
viewHolder.setTitle(model.getType().toString());
viewHolder.setImage(model.getUri(),getApplicationContext());
// viewHolder.setSeverity(model.getSeverity().toString());
viewholder.setLocation(getApplicationContext(),model.getLongitude(),model.getLatitude());
viewHolder.setDate(model.getTime());
viewHolder.setBrief(model.getBrief());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(Display.this).inflate(R.layout.sighting_rows, parent, false);
return new ViewHolder(view);
}
};
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
搜索功能
private void search(String searchText)
{
Query newQuery = mDatabase.child("Reference").orderByChild("type").startAt(searchText).endAt(searchText + "uf8ff");
options= new FirebaseRecyclerOptions.Builder<New>().setQuery(newQuery, New.class).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<New, Viewholder>(options) {
@Override
protected void onBindViewHolder(@NonNull ViewHolder viewHolder, int position, @NonNull New model) {
viewHolder.setTitle(model.getType().toString());
viewHolder.setImage(model.getUri(),getApplicationContext());
// viewHolder.setSeverity(model.getSeverity().toString());
viewHolder.setLocation(getApplicationContext(),model.getLongitude(),model.getLatitude());
viewHolder.setDate(model.getTime());
viewHolder.setBrief(model.getBrief());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(Display.this).inflate(R.layout.sighting_rows, parent, false);
return new ViewHolder(view);
}
};
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
在创建选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchsightings, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.e("Search",newText);
search(newText);
return false;
}
});
return true;
}
试试这个,把开始听。
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
更改此设置:
View view = LayoutInflater.from(Display.this).inflate(R.layout.sighting_rows, parent, false);
对此:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sighting_rows, parent, false);
并将其更改为true
@Override
public boolean onQueryTextChange(String newText) {
Log.e("Search",newText);
search(newText);
return true; //true
}