【发布时间】:2021-06-12 10:17:29
【问题描述】:
我想在单击按钮时更改 Firestore 查询参数。例如,我的查询默认是这样的:
db.collection("Posts").whereEqualTo("postCategory", "Business").orderBy("createdAt", Query.Direction.DESCENDING);
但是当我单击“经济”按钮时,查询需要更改为此并显示不同的帖子结果:
db.collection("Posts").whereEqualTo("postCategory", "Economy").orderBy("createdAt", Query.Direction.DESCENDING);
我尝试创建名为 selectedCategory 的字符串,并在单击按钮时更改了值,但在 onCreateView 中调用它并没有任何效果,并且只调用一次。我还在 Firestore 控制台中创建了索引,因此它不是与索引相关的问题。我的片段中有 2 个回收站视图;其中一个拥有类别和其他节目帖子的按钮。我一直在研究这个问题一段时间,任何帮助表示赞赏。谢谢
编辑:
TrendCategoryTagsAdapter.java
public class TrendCategoryTagsAdapter extends FirestoreRecyclerAdapter<CategorySelection, TrendCategoryTagsAdapter.TrendCategoryTagsHolder> {
Context context;
CategoryTagClicked categoryTagClicked;
int row_index;
public TrendCategoryTagsAdapter(@NonNull FirestoreRecyclerOptions<CategorySelection> options, Context context, CategoryTagClicked categoryTagClicked) {
super(options);
this.context = context;
this.categoryTagClicked = categoryTagClicked;
}
@Override
protected void onBindViewHolder(@NonNull final TrendCategoryTagsHolder holder, final int position, @NonNull CategorySelection model) {
holder.categoryNameText.setText(model.getCategoryName());
holder.categoryNameContainer.setOnClickListener(view -> {
String categoryName = holder.categoryNameText.getText().toString();
categoryTagClicked.onCategoryClicked(categoryName);
row_index = position;
notifyDataSetChanged();
});
if (row_index == position) {
holder.categoryNameContainer.setBackground(ContextCompat.getDrawable(context, R.drawable.black_rounded_bg));
} else {
holder.categoryNameContainer.setBackground(ContextCompat.getDrawable(context, R.drawable.grey_rounded_bg));
}
}
@NonNull
@Override
public TrendCategoryTagsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.trendcategory_cell, parent, false);
return new TrendCategoryTagsAdapter.TrendCategoryTagsHolder(v);
}
public static class TrendCategoryTagsHolder extends RecyclerView.ViewHolder {
RelativeLayout categoryNameContainer;
TextView categoryNameText;
public TrendCategoryTagsHolder(@NonNull View itemView) {
super(itemView);
categoryNameContainer = itemView.findViewById(R.id.categoryNameContainer);
categoryNameText = itemView.findViewById(R.id.categoryNameText);
}
}
}
TrendingFragment.java
public class TrendingFragment extends Fragment implements CategoryTagClicked {
RecyclerView trendPostRV, trendingCategoryRV;
public TextView noPostTV;
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference categoryRef, postRef;
TrendCategoryTagsAdapter trendCategoryTagsAdapter;
TrendingPostAdapter trendingPostAdapter;
String selectedCategory = "Art";
public TrendingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_trending, container, false);
trendPostRV = view.findViewById(R.id.trendPostRV);
trendingCategoryRV = view.findViewById(R.id.trendingCategoryRV);
noPostTV = view.findViewById(R.id.noPostTV);
setUpTrendCategoryTagsRV();
setUpTrendingPostRV();
checkIfDataNull();
// Inflate the layout for this fragment
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
trendCategoryTagsAdapter.startListening();
trendingPostAdapter.startListening();
}
@Override
public void onDestroyView() {
super.onDestroyView();
trendCategoryTagsAdapter.stopListening();
trendingPostAdapter.stopListening();
}
private void setUpTrendCategoryTagsRV() {
categoryRef = db.collection("Categories");
Query query = categoryRef.orderBy("categoryName", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<CategorySelection> options = new FirestoreRecyclerOptions.Builder<CategorySelection>()
.setQuery(query, CategorySelection.class)
.build();
trendCategoryTagsAdapter = new TrendCategoryTagsAdapter(options, getContext(), this);
trendingCategoryRV.setNestedScrollingEnabled(false);
final LinearLayoutManager trendingTagsLM = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
trendingCategoryRV.setLayoutManager(trendingTagsLM);
trendingCategoryRV.setAdapter(trendCategoryTagsAdapter);
trendCategoryTagsAdapter.notifyDataSetChanged();
}
public void setUpTrendingPostRV() {
postRef = db.collection("Posts");
Query query = postRef.whereEqualTo("postCategory", selectedCategory).orderBy("createdAt", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Post> options = new FirestoreRecyclerOptions.Builder<Post>()
.setQuery(query, Post.class)
.build();
trendingPostAdapter = new TrendingPostAdapter(options, getContext());
trendPostRV.setNestedScrollingEnabled(false);
LinearLayoutManager trendingPostLM = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
trendPostRV.setLayoutManager(trendingPostLM);
trendPostRV.setAdapter(trendingPostAdapter);
trendingPostAdapter.notifyDataSetChanged();
}
private void checkIfDataNull() {
db.collection("Posts").whereEqualTo("postCategory", selectedCategory).get().addOnCompleteListener(task -> {
if (task.isSuccessful() && task.getResult() != null) {
if (task.getResult().size() == 0) {
noPostTV.setVisibility(View.VISIBLE);
trendPostRV.setVisibility(View.GONE);
} else {
noPostTV.setVisibility(View.GONE);
trendPostRV.setVisibility(View.VISIBLE);
}
} else {
Log.d("TAG", "Error getting documents: ", task.getException());
}
});
}
@Override
public void onCategoryClicked(String category) {
selectedCategory = category;
Toast.makeText(getContext(), selectedCategory, Toast.LENGTH_SHORT).show();
}
}
CategoryTagClicked.java(接口)
public interface CategoryTagClicked {
void onCategoryClicked(String category);
}
简要说明我的工作:
每当我单击类别按钮时,我都会将按钮的文本(在本例中为类别名称)传递给 TrendingFragment 中的 selectedCategory 字符串。 String selectedCategory 是 setUpTrendingPostRV 函数中查询的参数。问题是它只在更改片段后显示结果,但我希望在单击按钮后立即进行更改。
【问题讨论】:
-
创建一个公共 firestore 变量并使用不同的值运行每个查询。我不明白你的问题是什么!
-
好吧,我想创建动态查询,如上所述在单击按钮时更改参数
-
将您使用的最小逻辑显示为代码,而不是试图解释它,这将有助于理解您的代码问题 - 在不显示最小示例的情况下解释应该发生的事情会让人难以回答。跨度>
-
为什么说“没有效果”?向我们展示您在代码中的尝试。
-
您是从 onCreateView 调用 setUpTrendingPostRV(),而不是点击右键?那么为什么不添加一个点击监听器并从那里调用该方法呢?
标签: java android firebase google-cloud-firestore android-recyclerview