我正在使用FirestoreRecyclerAdapter
检索数据,完成后,我需要检查是否已检索到任何项目。我不知道该怎么做。
我从名为FragmentChartsList
的类中调用它,如下所示。这应该首先设置适配器,并将“名称”作为mOrder
的值。稍后,包含此片段的活动可以调用setOrderField()
,其值为mOrder
,该值是用户从微调框中选择的。
每次调用setOrderField()
时,都会创建一个新的适配器实例并将其附加到recyclerView。在这一点上,我需要检查适配器的新版本是否包含任何数据,并显示“未找到图表”消息,或显示检索到的图表(显然,如果列表仅在排序中,则显示该数目)。项目保持不变,但是我将对此进行扩展,以允许用户按不同的条件过滤图表,因此返回的图表数将发生变化。
当前,setOrderField()
调用refreshViewOnNewData()
,这应该查明正在显示多少个图表。如果为0,则应显示“未找到图表”消息;如果为0,则应显示包含图表的RecyclerView。
此刻,当我尝试计算图表时,总是得到0值。我怀疑这是因为适配器尚未完成从数据库中检索它们的操作,但是找不到任何可以添加某种“ onComplete
”侦听器的东西,以便使我知道它已经完成。
有人可以建议我如何实现这一目标吗?
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;
}
}
答案 0 :(得分:1)
解决了这个问题...我需要在查询上设置一个侦听器。
因此,我不再需要从上方refreshViewOnNewData
到setOrder
的呼叫,而是拥有:
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