Cannot retrieve data using FirebaseRecyclerOptions after upgrading to FirebaseUI 3.0

允我心安 提交于 2021-02-10 05:50:23

问题


I'm using FirebaseRecyclerOptions because I upgraded to the new FirebaseUI 3.0 version but now I cannot retrieve anything from the database. The same code used to work very well in the old FirebaseRecylcerAdapter method. It seems like it doesn't even enter onBindViewHolder.

Initializations

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 code

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:


Since version 3.0 of FirebaseUI, you need to explicitly call startListening() & stopListening() on the adapter to instruct it to start/stop retrieving data from the database.

From the Upgrading to FirebaseUI 3.0 guide:

Adapter Lifecycle - in previous versions the adapters began listening immediately upon instantiation and had a cleanup() call to stop listening. In 3.x you must explicitly call startListening() and stopListening() or pass a LifecycleOwner to the options builder.



来源:https://stackoverflow.com/questions/50703205/cannot-retrieve-data-using-firebaserecycleroptions-after-upgrading-to-firebaseui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!