FirebaseRecyclerView无法在片段内部工作

时间:2017-12-22 15:32:38

标签: java android firebase

OnCreateView内的代码

contactList = v.findViewById(R.id.contacts_list);
        contactList.setLayoutManager(new LinearLayoutManager(getActivity()));
        contactList.setItemAnimator(new DefaultItemAnimator());
        String UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
        Log.i("UID",UID);
        Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child("contacts").child(UID).child("contact").limitToLast(50);

        options = new FirebaseRecyclerOptions.Builder<ContactModel>()
                .setQuery(query, ContactModel.class)
                .build();


        contactList.setAdapter(new ContactsAdapter(options,getActivity()));

适配器类内的代码

 public class ContactsAdapter extends FirebaseRecyclerAdapter<ContactModel, Contacts.ViewHolder> {
        Context c;

        public ContactsAdapter(FirebaseRecyclerOptions<ContactModel> options, Context c) {
            super(options);
            this.c = c;

        }

        @Override
        protected void onBindViewHolder(final ViewHolder holder, int position, ContactModel model) {

        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.private_chat_item, parent, false);
            return new ViewHolder(v);
        }
}

参考没有问题。我在一个活动类中使用相同的引用,它的工作原理。

此外,我还使用旧版本的firebase recyclerview。它工作正常。那么问题出在哪里..

1 个答案:

答案 0 :(得分:1)

"GET actual"方法之前使用adapter.startListening();为我解决了问题。