Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

android - Can't get FirestoreRecyclerAdapter to show items

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...