无法让 FirestoreRecyclerAdapter 显示项目
Posted
技术标签:
【中文标题】无法让 FirestoreRecyclerAdapter 显示项目【英文标题】:Can't get FirestoreRecyclerAdapter to show items 【发布时间】:2018-10-11 16:15:06 【问题描述】:我正在更新我的应用以使用 Firebase 的 Firestore 数据库。我正在努力让应用程序显示从数据库中检索到的数据。数据检索正常,但不显示。通过设置断点,我已经确定 ViewHolder 在任何时候都没有绑定到适配器。
数据显示在片段中。 Fragment 布局是(我已经去掉了不相关的东西,比如填充、大小等):
<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如下:
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;
调用了 ChartListAdapter 构造函数,但从不调用 onBindViewHolder 和 onCreateViewHolder,并且从不访问 ChartViewHolder。我错过了一行代码吗?或者这样做完全错误?我对 Adapters 和 RecyclerViews 不是很熟悉,所以我发现很难把它们放在一起。
【问题讨论】:
【参考方案1】:对于那些从 Google 来到这里的人,请在选项上设置生命周期所有者,以便自动调用开始/停止侦听。
FirestoreRecyclerOptions<Chart> recyclerOptions = FirestoreRecyclerOptions.Builder<Chart>()
.setQuery(mChartsQuery, Chart.class)
.setLifecycleOwner(this)
.build();
【讨论】:
【参考方案2】:您需要添加以下内容,开始监听数据,调用startListening()
方法。您可能想在您的 onStart()
方法中调用它。在调用 startListening()
之前,请确保您已完成读取数据所需的任何身份验证,否则您的查询将失败:
@Override
protected void onStart()
super.onStart();
adapter.startListening();
更多信息在这里:
https://github.com/firebase/FirebaseUI-Android/tree/master/firestore
【讨论】:
以上是关于无法让 FirestoreRecyclerAdapter 显示项目的主要内容,如果未能解决你的问题,请参考以下文章