无法使用FirebaseRecyclerOptions检索数据

无法使用FirebaseRecyclerOptions检索数据

本文介绍了升级到FirebaseUI 3.0后,无法使用FirebaseRecyclerOptions检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 FirebaseRecyclerOptions ,因为我已升级到新的FirebaseUI 3.0版本,但是现在我无法从数据库中检索任何内容.相同的代码在旧的 FirebaseRecylcerAdapter 方法中可以很好地工作.似乎甚至都没有输入 onBindViewHolder .

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.

初始化

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);

推荐答案

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

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.

升级到FirebaseUI 3.0 指南:

这篇关于升级到FirebaseUI 3.0后,无法使用FirebaseRecyclerOptions检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:20