我正在使用Firebase Adapter和Recyclerview对Firebase数据库执行一些查询。如果我的数据库中有带有edittext的结果,那么我将使用recyclerview查看它们。那是我的问题。如果我用Edittext调用的查询未包含在数据库中,我想在屏幕上显示祝酒词。我已经写了一些代码,但是我不能完全运行它。 Stackoverflow中其他问题的答案对我不起作用,我无法理解问题。请在下面查看我的代码。如果您能提供帮助,我将不胜感激。对不起,我的英语不好。
MainActivity
public class MainActivity extends AppCompatActivity {
DatabaseReference mUserDatabase;
EditText Search_Edit_Text;
ImageButton Search_Button;
RecyclerView Search_Contact_List;
FirebaseRecyclerOptions<User> options;
FirebaseRecyclerAdapter<User,CategoryViewHolder> adapter;
Query firebaseSearchQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Database");
Search_Edit_Text = (EditText) findViewById(R.id.Search_Edit_Text);
Search_Button = (ImageButton) findViewById(R.id.Search_Button);
Search_Contact_List = (RecyclerView) findViewById(R.id.Search_Contact_List);
Search_Contact_List.setHasFixedSize(true);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getBaseContext(),1);
Search_Contact_List.setLayoutManager(gridLayoutManager);
Search_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String searchText = Search_Edit_Text.getText().toString().trim();
firebaseUserSearch(searchText);
}
});
}
private void firebaseUserSearch(String searchText) {
firebaseSearchQuery = mUserDatabase.orderByChild("phone").startAt("" + searchText).endAt("" + searchText + "\uf8ff");
options = new FirebaseRecyclerOptions.Builder<User>()
.setQuery(firebaseSearchQuery,User.class)
.build();
adapter = new FirebaseRecyclerAdapter<User, CategoryViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final CategoryViewHolder holder, int position, @NonNull final User model) {
holder.categoryname.setText(model.getName());
}
@NonNull
@Override
public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_layout,parent,false);
return new CategoryViewHolder(itemView);
}
};
setCategory();
}
private void setCategory() {
adapter.startListening();
Search_Contact_List.setAdapter(adapter);
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
checkEmpty();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
checkEmpty();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
checkEmpty();
}
void checkEmpty() {
if (adapter.getItemCount() == 0){
Toast.makeText(MainActivity.this, "No Result", Toast.LENGTH_SHORT).show(); //This code does not work.
} else {
Toast.makeText(MainActivity.this, "OKEY", Toast.LENGTH_SHORT).show(); //This code works.
}
}
});
}
}
我写道,上面的某些代码有效。但是当我没有得到任何结果时,代码将无法正常工作。一些Stackoverflow回答说,我需要使用adapter.getCount()或adapter.getItemCount()来获取一个值,但是我尝试了一下,但是没有用。
CategoryViewHolder.class
public class CategoryViewHolder extends RecyclerView.ViewHolder {
public TextView categoryname;
public CategoryViewHolder(View itemView) {
super(itemView);
categoryname = (TextView)itemView.findViewById(R.id.Search_Result_Name);
}
}
User.class
public class User {
public String name;
public String phone;
public User() {
}
public User(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}