0

firebase Reference에서 Food 개체를 읽고 음식 목록에 추가하려고하지만 목록에 아무것도 추가되지 않았습니다. 지금까지 내 코드가 무엇입니까? 왜 데이터가 목록에 추가되지 않는지에 대한 도움이 필요합니까?내 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은 비동기 방식이므로 즉시 결과를 얻지 못할 수 있습니다. 따라서 데이터가 변경되는 정확한 순간에 ListonDataChange 방법으로 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); 
    } 
}); 

희망합니다.

관련 문제