我的应用允许Users
创建Charts
,并将其存储在Firestore数据库中,如下所示:
charts
> documentKey1
> description = "Example description"
> name = "Chart name 1"
> uId = user1Id_string
> documentKey2
> description = "Another example description"
> name = "Chart name 2"
> uId = user2Id_string
> documentKey3
> description = "Example description again"
> name = "Chart name 3"
> uId = user1Id_string
每个用户可以拥有多个图表。目前,我的MainActivity
从数据库中检索所有当前用户的图表,将它们按给定的顺序(由用户设置)排序,并在Fragment中的FirebaseRecyclerView中显示它们。
MainActivity在ViewPager中有2个片段。相关的是FragmentMyCharts,其中包含:
public class FragmentMyCharts extends FragmentChartsList {
public FragmentMyCharts() {}
public Query getQuery(FirebaseFirestore databaseReference,
String orderBy,
Query.Direction direction) {
// Specify the query which is used to retrieve this user's charts
return databaseReference.collection("charts")
.whereEqualTo("uid", getUid())
.orderBy(orderBy, direction);
}
}
这扩展了FragmentChartsList,如下所示:
public abstract class FragmentChartsList extends Fragment {
private FirebaseFirestore mDatabaseRef;
private ChartListAdapter mAdapter;
private RecyclerView mRecycler;
private String mOrder = "name", mFilter;
private Query.Direction mDirection = Query.Direction.DESCENDING;
private TextView mLoadingList, mEmptyList;
public FragmentChartsList() {}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Inflate layout, and find Recycler View which will hold the list
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();
setOrderAndFilter(mOrder, mDirection, mFilter);
return rootView;
}
public abstract Query getQuery(FirebaseFirestore databaseReference,
String orderBy,
Query.Direction direction);
public void setOrderAndFilter(String order, Query.Direction direction, String filterString) {
mOrder = order;
mDirection = direction;
mFilter = filterString;
Query mChartsQuery = getQuery(mDatabaseRef, mOrder, mDirection);
// Update recycler options
FirestoreRecyclerOptions<Chart> recyclerOptions = new FirestoreRecyclerOptions.Builder<Chart>()
.setQuery(mChartsQuery, Chart.class)
.build();
mAdapter = new ChartListAdapter(recyclerOptions, getActivity());
mAdapter.startListening();
mRecycler.setAdapter(mAdapter);
mChartsQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (queryDocumentSnapshots != null) {
// Hide "loading lists" text
mLoadingList.setVisibility(View.GONE);
if(queryDocumentSnapshots.size() > 0) {
mEmptyList.setVisibility(View.GONE);
mRecycler.setVisibility(View.VISIBLE);
}else {
// If number of Charts = 0,
// if we have filtered, show "no results"
// otherwise, show "no charts"
mEmptyList.setVisibility(View.VISIBLE);
mRecycler.setVisibility(View.GONE);
}
}
}
});
}
这引用了ChartListAdapter
类,其定义如下:
public class ChartListAdapter extends FirestoreRecyclerAdapter<Chart, ChartViewHolder> {
private Activity mActivity;
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);
}
}
因此,当前,这会检索当前用户的所有图表,并对它们进行排序和显示。我想做的就是过滤结果,以便用户可以输入一些搜索文本,并且只显示名称或描述包含该文本的图表。
我的理解是,我无法在Firestore查询中执行此操作,所以我的想法是检索完整的图表列表,然后遍历所有图表,删除所有不符合条件的图表,然后然后显示其余的(列表中不太可能有数百个图表;大多数用户大约有10个)。
我不知道该怎么做,我甚至不确定这是否是最好的方法。这样做的显而易见的地方似乎是当我将snapshotListener
的{{1}}添加到ChartsQuery
时,但是我找不到从列表中删除单个快照的方法。或者,我可以将所有项目放入FragmentChartsList
中,然后删除不需要的项目吗?
有人能指出我正确的方向吗?我在网上找到的所有内容似乎都与删除recyclerView项并将其从数据库中删除有关,这是我不想做的。
答案 0 :(得分:2)
您可以将以下查询用于搜索过滤器功能。
databaseReference.collection("charts")
.whereEqualTo("uid", getUid())
.orderBy("name")
.startAt(searchText)
.endAt(searchtText + "\uf8ff");
将搜索文本传递给startAt()和endAt()函数,名称是您的documentField。
并将此查询传递给firestoreAdapter,它将为您提供结果记录。