FirebaseRecyclerAdapter不在recyclerview上呈现任何内容

时间:2019-01-24 17:40:01

标签: android firebase firebase-realtime-database

enter image description here我已经检查了每一段代码,以便从Firebase成功地在recyclerview上呈现数据,但仍然无法解决问题。

当我使用Log.d获取快照的子代时,logcat上没有任何显示(均行未执行)。任何人都可以指出错误。

public class MyOrders extends AppCompatActivity {

private static final String TAG = "MyOrders";
DatabaseReference databaseReference;
FirebaseRecyclerAdapter firebaseRecyclerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_orders);

    RecyclerView myOrderList = findViewById(R.id.myOrderList);

    Query query = FirebaseDatabase.getInstance().getReference("Requests").orderByChild("name").equalTo(User.currentUser.getUserName());

    FirebaseRecyclerOptions<Request> options = new FirebaseRecyclerOptions.Builder<Request>().setQuery(query, new SnapshotParser<Request>() {
        @NonNull
        @Override
        public Request parseSnapshot(@NonNull DataSnapshot snapshot) {
            Log.d(TAG, "onCreate: "+snapshot.getChildrenCount());

            return new Request(snapshot.child("name").getValue().toString(), snapshot.child("phone").getValue().toString(), snapshot.child("address").getValue().toString(), snapshot.child("total").getValue().toString());
        }
    }).build();


    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Request, MyOrdersViewHolder>(options){
        @NonNull
        @Override
        public MyOrdersViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_order_template, viewGroup, false);
            return new MyOrdersViewHolder(v);
        }

        @Override
        protected void onBindViewHolder(@NonNull MyOrdersViewHolder holder, int position, @NonNull Request model) {
            Log.d(TAG, "onCreate: "+position);
            holder.setOrder_id(firebaseRecyclerAdapter.getRef(position).getKey());
            holder.setOrder_status(model.getOrderStatus());
            holder.setOrder_items(model.getRequestedOrderList().size()+"");
            holder.setItemClickLitener(new ItemClickLitener() {
                @Override
                public void onClick(View v, int pos, boolean isLongClick) {
                    Toast.makeText(MyOrders.this, ""+"Pending Action", Toast.LENGTH_SHORT).show();
                }
            });

        }
    };
    myOrderList.setAdapter(firebaseRecyclerAdapter);

}

@Override
protected void onStart() {
    super.onStart();
    firebaseRecyclerAdapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    firebaseRecyclerAdapter.stopListening();
}

}

0 个答案:

没有答案