我正在尝试从我的firebase参考中读取一个Food对象并将其添加到食物列表中,但没有任何内容添加到列表中。到目前为止,这是我的代码,为什么没有数据被附加到列表的任何帮助?
public class BrekoFragment extends Fragment{
private RecyclerView recyclerView;
private FoodsAdapter adapter;
private List<Food> foodList;
private DatabaseReference mFoodReference;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.breakfast_fragment,null);
recyclerView = (RecyclerView)view.findViewById(R.id.break_fast_foods);
foodList=new ArrayList<>();
adapter=new FoodsAdapter(view.getContext(),foodList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
mFoodReference= FirebaseDatabase.getInstance().getReference().child("BreakFast").child("Coffee");
final FloatingActionButton fab=MainActivity2.fab;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if (dy > 0 ||dy<0 && fab.isShown())
{
fab.hide();
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
if (newState == RecyclerView.SCROLL_STATE_IDLE)
{
fab.show();
}
super.onScrollStateChanged(recyclerView, newState);
}
});
prepareFoodData();
return view;
}
private void prepareFoodData() {
Food Rice=new Food("Rice","Rice is the seed of the grass species Oryza sativa or Oryza glaberrima. As a cereal grain, it is the most widely consumed staple food for a large part of the world's human population, especially in Asia",12,"http://2erape3gkyv5ojcr3ljlepou.wpengine.netdna-cdn.com/wp-content/uploads/cache/2015/10/veg-fried-rice/554794907.jpg");
//foodList.add(Rice);This Works
Food Tea=new Food("Coffee","Any of various tropical African shrubs or trees of the genus Coffea, especially C. arabica or C. canephora, widely cultivated in the tropics for their seeds that are dried, roasted, and ground to prepare a stimulating aromatic drink.",5,"http://www.ocean985.com/wp-content/blogs.dir/18/files/coffee2-637x416.jpg");
//foodList.add(Tea);//This Works
Log.e("FoodReference",mFoodReference+" ");
mFoodReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Food food=dataSnapshot.getValue(Food.class);
foodList.add(food);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("FoodList"," "+databaseError);
}
});
Log.e("FoodList",foodList.toString());
}
}
如果我直接将数据添加到列表中,则可以正常工作。
答案 0 :(得分:0)
在家里认识到这是一个互联网质量差的问题。抱歉。
答案 1 :(得分:0)
由于ValueEventListener
是一种异步方法,因此可能无法立即获得结果。因此,您需要在List
方法中使用其他单词在更改数据的确切时刻声明onDataChange
。比你的List
充满数据还要多。
mFoodReference.addValueEventListener(new ValueEventListener() {
List<Food> foodList = new ArrayList<>();
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Food food=dataSnapshot.getValue(Food.class);
foodList.add(food);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("FoodList"," "+databaseError);
}
});
希望它能够实现。