Can't get FirestoreRecyclerAdapter to show items

一笑奈何 提交于 2020-05-15 08:27:05

问题


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.

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>

The Fragment code itself is:

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 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:

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

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.


回答1:


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

more info here:

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




回答2:


For those who came here from Google, set the lifecycle owner on the options so that start/stop listening is called automatically.

FirestoreRecyclerOptions<Chart> recyclerOptions = FirestoreRecyclerOptions.Builder<Chart>()
            .setQuery(mChartsQuery, Chart.class)
            .setLifecycleOwner(this)
            .build();


来源:https://stackoverflow.com/questions/50119627/cant-get-firestorerecycleradapter-to-show-items

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