无法让FirestoreRecyclerAdapter显示项目

无法让FirestoreRecyclerAdapter显示项目

本文介绍了无法让FirestoreRecyclerAdapter显示项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新我的应用程序以使用Firebase的Firestore数据库.我正在努力让该应用程序显示从数据库中检索到的数据.可以正确检索数据,但不会显示.通过设置断点,我已经确定ViewHolder在任何时候都没有绑定到适配器.

I'm updating my app to use Firebase's Firestore database. I'm struggling to get the app to show the data that's been retrieved from the database. The data is retrieved ok, but doesn't show up. By setting breakpoints, I've established that the ViewHolder isn't being bound to the adapter at any point.

数据以片段形式显示. Fragment的布局是(我取出了无关紧要的内容,例如填充,大小等):

The data is being shown in a Fragment. The Fragment layout is (I've taken out irrelevant stuff like padding, sizes etc):

<android.support.constraint.ConstraintLayout
    android:id="@+id/layout_charts_list"
    tools:context="apppath.fragments.ChartListFragment">

    <TextView
        android:id="@+id/loading_list"
        android:text="@string/loading_my_charts" />

    <TextView
        android:id="@+id/empty_list"
        android:text="@string/my_charts_empty"
        android:visibility="gone"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/charts_list" />

</android.support.constraint.ConstraintLayout>

片段代码本身是:

public abstract class ChartListFragment extends Fragment {

    private FirebaseFirestore mDatabaseRef;
    private FirestoreRecyclerAdapter<Chart, ChartViewHolder> mAdapter;
    private Query mChartsQuery;
    private RecyclerView mRecycler;

    public ChartListFragment() {}

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

        View rootView = inflater.inflate(
                R.layout.fragment_charts_list, container, false);

        mRecycler = rootView.findViewById(R.id.charts_list);
        mRecycler.setHasFixedSize(true);

        // Set up Layout Manager, and set Recycler View to use it
        LinearLayoutManager mManager = new LinearLayoutManager(getActivity());
        mManager.setReverseLayout(true);
        mManager.setStackFromEnd(true);
        mRecycler.setLayoutManager(mManager);

        // Connect to the database, and get the appropriate query (as set in the actual fragment)
        mDatabaseRef = FirebaseFirestore.getInstance();
        mChartsQuery = getQuery(mDatabaseRef);

        // Set up Recycler Adapter
        FirestoreRecyclerOptions<Chart> recyclerOptions = new FirestoreRecyclerOptions.Builder<Chart>()
                .setQuery(mChartsQuery, Chart.class)
                .build();

        mAdapter = new ChartListAdapter(recyclerOptions);

        // Use Recycler Adapter in RecyclerView
        mRecycler.setAdapter(mAdapter);

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Add listener to charts collection, and deal with any changes by re-showing the list
        mChartsQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
                                @Nullable FirebaseFirestoreException e) {

                if (queryDocumentSnapshots != null && queryDocumentSnapshots.isEmpty()) {
                    ((MainActivity) getActivity()).setPage(1);
                    mRecycler.setVisibility(View.GONE);
                }else {
                    mRecycler.setVisibility(View.VISIBLE);
                }

            }
        });
    }


    // HELPER FUNCTIONS

    public abstract Query getQuery(FirebaseFirestore databaseReference);

}

ChartListAdapter如下:

ChartListAdapter is as follows:

public class ChartListAdapter
        extends FirestoreRecyclerAdapter<Chart, ChartViewHolder> {

    public ChartListAdapter(FirestoreRecyclerOptions recyclerOptions) {
        super(recyclerOptions);

    }

    @Override
    protected void onBindViewHolder(ChartViewHolder holder, int position, Chart model) {
        holder.setChartName(model.getName());

        // Bind Chart to ViewHolder
        holder.bindToChart(model);
    }

    @Override
    public ChartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_chart, parent, false);

        return new ChartViewHolder(view);
    }
}

ChartViewHolder:

ChartViewHolder:

public class ChartViewHolder extends RecyclerView.ViewHolder {

    private TextView chartNameView;
    private String chartKey;

    public ChartViewHolder(View itemView) {
        super(itemView);

        chartNameView = itemView.findViewById(R.id.chart_name);
    }

    public void setChartName(String chartName) {
        chartNameView.setText(chartName);
    }

    public void bindToChart(Chart chart) {
        chartKey = chart.getKey();
        chartNameView.setText(chart.getName());
    }

    public String getChartKey() {
        return chartKey;
    }
}

将调用ChartListAdapter构造函数,但永远不会调用onBindViewHolder和onCreateViewHolder,也永远不会访问ChartViewHolder.我是否缺少一行代码?还是这样做完全错误?我对Adapters和RecyclerViews并不是很熟悉,所以我发现很难将它们放在一起.

The ChartListAdapter constructor is called, but onBindViewHolder and onCreateViewHolder are never called, and ChartViewHolder is never accessed at all. Am I missing a line of code? Or doing this completely wrong? I'm not all that familiar with Adapters and RecyclerViews, so I've found it quite hard to get to grips with putting it all together.

推荐答案

您需要添加以下内容,才能开始侦听数据,请调用startListening()方法.您可能想在您的onStart()方法中调用它.在调用startListening()之前,请确保已完成读取数据所需的所有身份验证,否则查询将失败:

You need to add the following, to begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail:

@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}

更多信息在这里:

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

这篇关于无法让FirestoreRecyclerAdapter显示项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:19