我正在尝试从Firebase实时数据库中读取“特定”数据。我已经看到了一些示例,这些示例从表中获取所有数据,然后遍历该表以查找所需的数据记录。 下面是我如何保存数据的方法
FirebaseDatabase database;
DatabaseReference foodList;
String categoryId="";
FirebaseRecyclerAdapter<Food,FoodViewHolder>adapter;
FirebaseRecyclerAdapter<Food,FoodViewHolder>searchAdapter;
List<String> suggestList=new ArrayList<>();
MaterialSearchBar materialSearchBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
database=FirebaseDatabase.getInstance();
foodList=database.getReference("Foods");
recyclerView=findViewById(R.id.recycler_food);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
if(getIntent()!=null)
{
categoryId=getIntent().getStringExtra("CategoryId");
}
if(!categoryId.isEmpty() )
{
loadListFood(categoryId);
}
materialSearchBar=findViewById(R.id.searchBar);
materialSearchBar.setHint("Enter food name");
loadSuggestion();
materialSearchBar.setLastSuggestions(suggestList);
materialSearchBar.setCardViewElevation(10);
materialSearchBar.addTextChangeListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
List<String>suggest=new ArrayList<>();
for (String search:suggestList)
{
if(search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
suggest.add(search);
}
materialSearchBar.setLastSuggestions(suggest);
}
@Override
public void afterTextChanged(Editable s) {
}
});
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
@Override
public void onSearchStateChanged(boolean enabled) {
if(!enabled)
{
recyclerView.setAdapter(adapter);
}
}
@Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text);
}
@Override
public void onButtonClicked(int buttonCode) {
}
});
}
private void startSearch(CharSequence text) {
searchAdapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(
Food.class,
R.layout.food_item,
FoodViewHolder.class,
foodList.orderByChild("Name").equalTo(text.toString())// compare Food Name//////
) {
@Override
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
viewHolder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.food_image);
final Food local=model;
viewHolder.setItemClickListner(new ItemClickListner() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(FoodList.this,""+local.getName(),Toast.LENGTH_SHORT).show();//there is problem local.getName???????????
//Start new Acitvity
Intent foodDetail=new Intent(FoodList.this,FoodDetail.class);
foodDetail.putExtra("FoodId",searchAdapter.getRef(position).getKey());//send Food ID to new Acitivity
startActivity(foodDetail);
}
});
}
};
recyclerView.setAdapter(searchAdapter);
}
private void loadSuggestion() {
foodList.orderByChild("MenuId").equalTo(categoryId)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren() )
{
Food item=postSnapshot.getValue(Food.class);
suggestList.add(item.getName());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void loadListFood(String categoryId) {
adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class,
R.layout.food_item,
FoodViewHolder.class,
foodList.orderByChild("menuId").equalTo(categoryId))
{
@Override
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
viewHolder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.food_image);
final Food local=model;
viewHolder.setItemClickListner(new ItemClickListner() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(FoodList.this,""+local.getName(),Toast.LENGTH_SHORT).show();
Intent foodDetail=new Intent(FoodList.this,FoodDetail.class);
foodDetail.putExtra("FoodId",adapter.getRef(position).getKey());
startActivity(foodDetail);
}
});
}
};
recyclerView.setAdapter(adapter);
}
}
更新
当我尝试从Firebase获取数据以更新食物清单时,我没有发现要更改的数据库。下面是我要更新的数据库。