gpt4 book ai didi

Android Firebase orderByChild 查询

转载 作者:行者123 更新时间:2023-11-29 14:44:54 26 4
gpt4 key购买 nike

我目前正在使用 Firebase 编写 Android 应用程序。我有一个具有城市属性的产品模型。我正在运行以下查询:

mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.keepSynced(true);

productQuery = mDatabaseReference.child("products")
.orderByChild("city").equalTo(mCurrentUser).limitToFirst(100);

此查询检索数据库中的所有产品,而不考虑城市。

在过去的四个小时里,我一直在审查关于 SO 的类似问题,并尝试了查询函数的不同组合 + 规则索引,但无济于事。我有一个我称之为愚蠢的解决方案,它在城市产品树下重新复制数据。不过,我猜测有更好的方法利用 orderByChild,如果我能得到一些帮助来按城市查询数据,我将不胜感激。

感谢您的参与

这是 CityFeed fragment - 注意:我检查过 mCurrentUserCity 已设置

public class CityFeedFragment extends BaseFragment {
private RecyclerView mRecyclerView;
private String mCurrentUserCity;
private FirebaseProductAdapter mAdapter;
private DatabaseReference mDatabaseReference;
private LinearLayoutManager mManager;
private Query productQuery;
private SwipeRefreshLayout mSwipeRefreshLayout;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_feed, container, false);
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.keepSynced(true);

retrieveCity();
fetchProducts();

mAdapter = new FirebaseProductAdapter(Product.class,
R.layout.feed_item, ProductViewHolder.class,
productQuery.getRef(), getContext(), false);

return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
mRecyclerView.setAdapter(mAdapter);

mManager = new LinearLayoutManager(getActivity());
mManager.setReverseLayout(true);
mManager.setStackFromEnd(true);

mRecyclerView.setHasFixedSize(true);

mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(mManager);


mSwipeRefreshLayout.setOnRefreshListener(() -> {
fetchProducts();
mAdapter.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
});

}


private void fetchProducts() {
productQuery = mDatabaseReference.child("products").orderByChild("city").equalTo(mCurrentUserCity);
}

private void retrieveCity() {
mDatabaseReference.child("users").child(getUid()).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if (user == null) {
showToast(getResources().getString(R.string.empty_user));
} else {
mCurrentUserCity = user.getCity();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
showToast(databaseError.toString());
Log.w("Cityfeed", "getUser:onCancelled", databaseError.toException());
}
}
);
}

}

数据结构 enter image description here

最佳答案

除了添加一个监听器以获取值作为数据快照外,您已完成所有操作。请用这个替换你的方法-

private void fetchProducts() {
productQuery = mDatabaseReference.child("products").orderByChild("city").equalTo(mCurrentUserCity).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Map of all products which has city equal to mCurrentUserCity
}
@Override
public void onCancelled(DatabaseError databaseError) {

}
}
);
}

关于Android Firebase orderByChild 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42423779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com