我的片段在RecycleView中没有收到任何值

时间:2019-12-02 01:32:50

标签: android firebase android-recyclerview google-cloud-firestore fragment

以前,我可以在我的应用程序中成功列出来自Cloud Firestore的数据。但是现在我正在尝试实现RecyclerView,该列表列出了片段中的数据,但没有得到结果。该片段显示为空结果。这是我的代码

ReviewFragment.java

public class ReviewFragment extends Fragment {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference userRef = db.collection("Users");
    private UserAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_review,container,false);
        Query query = userRef.orderBy("name",Query.Direction.ASCENDING);
        FirestoreRecyclerOptions<Userz> options = new FirestoreRecyclerOptions.Builder<Userz>()
                .setQuery(query,Userz.class)
                .build();
        adapter = new UserAdapter(options);
        RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.rvReview);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        return view;
    }

}

UserAdapter.java

public class UserAdapter extends FirestoreRecyclerAdapter <Userz,UserAdapter.UserHolder>{


    /**
     * Create a new RecyclerView adapter that listens to a Firestore Query.  See
     * {@link FirestoreRecyclerOptions} for configuration options.
     *
     * @param options
     */
    public UserAdapter(FirestoreRecyclerOptions<Userz> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(UserHolder userHolder, int i, Userz userz) {
        userHolder.textViewName.setText(userz.getName());
        userHolder.textViewStatus.setText(userz.getStatus());
        userHolder.textViewMessage.setText(userz.getMessages());
    }

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

    class UserHolder extends RecyclerView.ViewHolder{
        TextView textViewName;
        TextView textViewMessage;
        TextView textViewStatus;
        public UserHolder(View itemView) {
            super(itemView);
            textViewName = itemView.findViewById(R.id.name);
            textViewMessage = itemView.findViewById(R.id.messages);
            textViewStatus = itemView.findViewById(R.id.status);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

FirestorePagingAdapter生命周期 开始/停止聆听 FirestorePagingAdapter侦听滚动事件并仅在需要时从数据库加载其他页面。

要开始填充数据,请调用startListening()方法。您可能要在onStart()方法中调用它。在调用startListening()之前,请确保已完成读取数据所需的所有身份验证,否则查询将失败。

protected void onStart() {
    super.onStart();
    adapter.startListening();
}
Similarly, the stopListening() call freezes the data in the RecyclerView and prevents any future loading of data pages.

Call this method when the containing Activity or Fragment stops:

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

自动聆听 如果您不想手动开始/停止收听,则可以使用Android体系结构组件自动管理FirestorePagingAdapter的生命周期。 将LifecycleOwner传递给FirestorePagingOptions。Builder#setLifecycleOwner(...),FirebaseUI将自动启动和停止监听onStart()和onStop()。

Github https://github.com/firebase/FirebaseUI-Android/tree/master/firestore

答案 1 :(得分:0)

适配器类中没有getCount()方法。实现列表的方法和返回大小。