FirestoreRecyclerAdapter-如何知道何时检索数据? [英] FirestoreRecyclerAdapter - how do I know when data has been retrieved?
问题描述
我正在使用 FirestoreRecyclerAdapter
检索数据,完成后,我需要检查是否已检索到任何项目.我不知道该怎么做.
I'm retrieving data using a FirestoreRecyclerAdapter
, and, on completion, I need to check whether any items have been retrieved or not. I can't figure out how to do this.
我从名为 FragmentChartsList
的类中调用它,如下所示.这应该首先设置适配器,并使用名称"作为 mOrder
的值.稍后,包含此片段的活动可以调用 setOrderField()
,并使用其他值 mOrder
,该值是用户从微调器中选择的.
I'm calling it from a class called FragmentChartsList
, shown below. This should set up the adapter initially, with "name" as the value for mOrder
. Later, the Activity which contains this Fragment can call setOrderField()
with a different value of mOrder
, which the user has selected from a Spinner.
每次调用 setOrderField()
时,都会创建一个新的适配器实例并将其附加到recyclerView.在这一点上,我需要检查适配器的新版本是否包含任何数据,并显示未找到图表"消息,或显示检索到的图表(显然,如果列表只是在排序,则显示的数目).项目保持不变,但我将对此进行扩展,以允许用户按不同条件过滤图表,因此返回的图表数将发生变化.
Each time setOrderField()
is called, a new adapter instance is created and attached to the recyclerView. At this point I need to check whether the new version of the adapter contains any data, and either show a "no Charts found" message, or show the Charts which were retrieved (obviously if the list is just being sorted, then the number of items remains the same, but I'm going to be expanding this to allow the user to filter the Charts by different criteria, so the number of Charts returned will change).
当前, setOrderField()
调用 refreshViewOnNewData()
,这应该找出正在显示的图表数量.如果为0,则应显示未找到图表"消息;如果为0,则应显示包含图表的RecyclerView.
Currently, setOrderField()
calls refreshViewOnNewData()
, which should find out how many Charts are being shown; if it's 0, it should show the "no Charts found" message, and if it's >0 it should show the RecyclerView containing the Charts.
此刻,当我尝试计算图表时,总是得到0值.我怀疑这是因为适配器尚未完成从数据库中检索它们的操作,但是找不到任何可以添加某种" onComplete
"侦听器的东西,以便使我知道它已经完成了.
At the moment, I'm always getting a value of 0 when I try to count the Charts. I suspect it's because the adapter hasn't finished retrieving them from the database yet, but I can't find anything that allows me to add some kind of "onComplete
" listener so that I know it's finished.
有人可以建议我如何实现这一目标吗?
Can anyone suggest how I can achieve this?
public abstract class FragmentChartsList extends Fragment {
private FirebaseFirestore mDatabaseRef;
private ChartListAdapter mAdapter;
private Query mChartsQuery;
private RecyclerView mRecycler;
private String mOrder = "name";
private TextView mLoadingList, mEmptyList;
public FragmentChartsList() {}
@Override
public View onCreateView(@NonNull 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);
mLoadingList = rootView.findViewById(R.id.loading_list);
mEmptyList = rootView.findViewById(R.id.empty_list);
// 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
mDatabaseRef = FirebaseFirestore.getInstance();
setOrderField(mOrder); // Initialised to "name"
return rootView;
}
@Override
public void onStart() {
super.onStart();
mAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mAdapter.stopListening();
}
@Override
public void onDestroy() {
super.onDestroy();
mAdapter.stopListening();
}
// HELPER FUNCTIONS
public void setOrderField(String order) {
mOrder = order;
mChartsQuery = getQuery(mDatabaseRef, mOrder);
// Update recycler options
FirestoreRecyclerOptions<Chart> recyclerOptions = new FirestoreRecyclerOptions.Builder<Chart>()
.setQuery(mChartsQuery, Chart.class)
.build();
mAdapter = new ChartListAdapter(recyclerOptions, getActivity());
mAdapter.startListening();
mRecycler.swapAdapter(mAdapter, true);
refreshViewOnNewData();
}
private void refreshViewOnNewData() {
// Hide "loading" text
mLoadingList.setVisibility(View.GONE);
// Check number of charts being shown
//if (mAdapter != null && (mAdapter.getCount() > 0)) {
// If > 0, show Charts
mEmptyList.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
} else {
// If number of Charts = 0
// show "no charts"
mEmptyList.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
适配器类如下:
public class ChartListAdapter extends FirestoreRecyclerAdapter<Chart, ChartViewHolder> {
private Activity mActivity;
private int mCount;
public ChartListAdapter(FirestoreRecyclerOptions<Chart> recyclerOptions, Activity activity) {
super(recyclerOptions);
mActivity = activity;
}
@Override
protected void onBindViewHolder(@NonNull ChartViewHolder holder, int position, @NonNull Chart model) {
final String chartKey = this.getSnapshots().getSnapshot(position).getId();
model.setKey(chartKey);
// Set click listener for the chart
// On click, the user can view the chart
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mActivity, ActivityViewChart.class);
intent.putExtra("ChartKey", chartKey);
mActivity.startActivity(intent);
}
});
// Implement long-click menu
mActivity.registerForContextMenu(holder.itemView);
// Bind Chart to ViewHolder
holder.bindToChart(model);
}
@NonNull
@Override
public ChartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chart, parent, false);
return new ChartViewHolder(view);
}
@Override
public void onDataChanged() {
super.onDataChanged();
mCount = getItemCount();
}
public int getCount() {
return mCount;
}
}
推荐答案
解决了这个问题……我需要在查询中设置一个侦听器.
Figured this out... I needed to set a listener on the query instead.
因此,我不再需要从上面的 setOrder
中调用 refreshViewOnNewData
,而是有了:
So, instead of having the call to refreshViewOnNewData
from setOrder
above, I now have:
mChartsQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (queryDocumentSnapshots != null) {
mLoadingList.setVisibility(View.GONE);
if(queryDocumentSnapshots.size() > 0) {
mEmptyList.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
}else {
mEmptyList.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
});
}
还从适配器类中删除了 mCount
以及 getCount
和 onDataChanged
Also removed mCount
from the adapter class, along with getCount
and onDataChanged
这篇关于FirestoreRecyclerAdapter-如何知道何时检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!