嗨,我正在将Firebase数据库与搜索查询一起使用。我还使用离线方法从Firebase离线存储数据。但是在实现searchview后,当我打开页面时,它显示为空白。单击搜索图标后,数据即将到来。如何解决呢?我的应用程序在playstore上。 It is the Link of the Application
这是我的搜索查询代码
private void firebaseUserSearch(String searchText) {
Query firebaseSearchQuery = mUserDatabase.orderByChild("Title").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerAdapter<Blog,BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.bank_row,
BlogViewHolder.class,
firebaseSearchQuery
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, final Blog model, int position) {
final String fav_key=getRef(position).getKey();
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getExample());
viewHolder.setFavourite(fav_key);
viewHolder.favourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mfavourite=true;
databasefavourite.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(mfavourite){
if (dataSnapshot.child(fav_key).hasChild("Title")){
databasefavourite.child(fav_key).child("Title").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Title").setValue(model.getTitle());
mfavourite = false;
}
if (dataSnapshot.child(fav_key).hasChild("Description")){
databasefavourite.child(fav_key).child("Description").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Description").setValue(model.getDescription());
mfavourite = false;
}
if (dataSnapshot.child(fav_key).hasChild("Example")){
databasefavourite.child(fav_key).child("Example").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Example").setValue(model.getExample());
mfavourite = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
这是我的onStart()代码
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Blog,Idioms.BlogViewHolder>firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Blog, Idioms.BlogViewHolder>
(Blog.class,R.layout.bank_row,Idioms.BlogViewHolder.class,mUserDatabase) {
@Override
protected void populateViewHolder(Idioms.BlogViewHolder viewHolder, final Blog model, int position) {
final String fav_key=getRef(position).getKey();
viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getExample());
viewHolder.setFavourite(fav_key);
viewHolder.favourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mfavourite=true;
databasefavourite.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(mfavourite){
if (dataSnapshot.child(fav_key).hasChild("Title")){
databasefavourite.child(fav_key).child("Title").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Title").setValue(model.getTitle());
mfavourite = false;
}
if (dataSnapshot.child(fav_key).hasChild("Description")){
databasefavourite.child(fav_key).child("Description").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Description").setValue(model.getDescription());
mfavourite = false;
}
if (dataSnapshot.child(fav_key).hasChild("Example")){
databasefavourite.child(fav_key).child("Example").removeValue();
mfavourite = false;
}else {
databasefavourite.child(fav_key).child("Example").setValue(model.getExample());
mfavourite = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
这是我的搜索操作代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
firebaseUserSearch(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
firebaseUserSearch(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}