0

firebase ReferenceからFoodオブジェクトを読み込み、それを食品リストに追加しようとしていますが、何もリストに追加されていません。これまでのところ私のコードはありますが、なぜデータがリストに追加されていないのかについてのヘルプはありませんか?My Firebase ValueEventListenerがトリガーされていません

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

ValueEventListenerは非同期メソッドなので、すぐに結果を得ることはできません。したがって、データが変更された正確な時刻に、をonDataChangeメソッドにorher単語で宣言する必要があります。 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); 
    } 
}); 

希望です。

関連する問題