升级到FirebaseUI 3.0后无法使用FirebaseRecyclerOptions检索数据

时间:2018-06-05 14:59:29

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我正在使用FirebaseRecyclerOptions,因为我已升级到新的FirebaseUI 3.0版本,但现在我无法从数据库中检索任何内容。相同的代码过去在旧的FirebaseRecylcerAdapter方法中运行得非常好。似乎它甚至没有输入onBindViewHolder

初始化

linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        recyclerView = findViewById(R.id.main_recycler);
        recyclerView.hasFixedSize();
        recyclerView.setLayoutManager(linearLayoutManager);    
queryDatbase = mDatabase.child("chatlist").child(cUser);
        queryDatbase.keepSynced(true);
Query query = queryDatbase.orderByChild("time");
        query.keepSynced(true);

FirebaseRecyclerAdapter代码

firebaseoptions = new FirebaseRecyclerOptions.Builder<ConvModel>().setQuery(query,ConvModel.class).build();

    firebaseadapter = new FirebaseRecyclerAdapter<ConvModel, ChatlistHolder>(firebaseoptions) {
        @Override
        protected void onBindViewHolder(final ChatlistHolder holder, int position,final ConvModel model) {
            final String each_user_id = firebaseadapter.getRef(position).getKey();
            Toast.makeText(MainActivity.this, each_user_id, Toast.LENGTH_SHORT).show();
            //region SET EACH USER IMAGE and USERNAME
            assert each_user_id != null;
            mDatabase.child("users").child(each_user_id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    String dp = dataSnapshot.child("dp").getValue(String.class);
                    final String name = dataSnapshot.child("name").getValue(String.class);
                    holder.setName(name);
                    holder.setDp(dp,getApplicationContext());
                    // region OPEN CHAT ACTIVITY
                    holder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //open other activity

                        }
                    }); 
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(getApplicationContext(),databaseError.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });//endregion
        }

        @Override
        public ChatlistHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chatlist_single_item_layout,parent,false);
            return new ChatlistHolder(v);
        }
    };
    recyclerView.setAdapter(firebaseadapter);

1 个答案:

答案 0 :(得分:6)

从FirebaseUI 3.0版开始,您需要在适配器上显式调用startListening() & stopListening(),以指示它启动/停止从数据库中检索数据。

来自Upgrading to FirebaseUI 3.0指南:

  

适配器生命周期 - 在先前版本中,适配器开始监听   在实例化后立即进行cleanup()呼叫停止   听着。在3.x中,您必须明确调用startListening()和   stopListening()或将LifecycleOwner传递给选项构建器。