从两个不同的 Firebase 节点检索和设置 RecyclerView 数据
Retrieve and set RecyclerView data from two different Firebase nodes
我的主要目标是根据用户之前的操作将 RecyclerView 项目设置为 "favorited"。我可以在点击时存储和删除这些数据,但我很难在正确的时间在正确的地方显示它。
我正在使用两个不同的节点来实现这一点:
"quotes" : {
"0" : {
"Name" : "Foo",
"Source" : "Bar" },
"1" : {
"Name" : "Foo",
"Source" : "Bar" },
"2" : {
"Name" : "Foo",
"Source" : "Bar" }
},
"favorites" : {
"blah@blah,com" : {
"uid0" : "0"
"uid1" : "2" }}}
基本上我想做的是:
在 RecyclerView 中显示所有报价,如果它们的 ID 显示在该唯一用户的收藏夹中,则将其可视化设置为收藏夹。我在此处包含了一些似乎对我不起作用的代码。
private void bindHeart(final ItemHeartCardBinding binding) {
if (inProgress) {
animateProgress(binding);
} else {
binding.favorite.setImageResource(R.drawable.favorite_state_list);
}
//Loop through users favorites to see if current item exists
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
String email = "";
if (user != null) {
email = user.getEmail();
email = email.replace(".", ",");
}
final DatabaseReference favoriteRef = mRootRef.child("favorites/" + email);
//quoteKeyStringResId is passed in here as each RecyclerView item is being created. It's the uid of each quote.
final Query queryRef = favoriteRef.orderByValue().startAt(quoteKeyStringResId).endAt(quoteKeyStringResId);
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue().toString().equals(quoteKeyStringResId)) {
binding.favorite.setChecked(checked);
} else {
binding.favorite.setChecked(!checked);
}
}
我仔细研究了 Firebase database samples 并找到了一种更好的数据建模方法。在同一节点中包含所有相关数据允许我同时查询所有数据,而不必担心保持 RecyclerView 项目的顺序相同。事情并没有这么浅薄,但我认为没关系:
"quotes" : {
"0" : {
"Name" : "Foo",
"Source" : "Bar",
//Moved this from a separate node into here
"Favorites" : {
//Each user will be listed if favorited, and removed if unfavorited, making it easy to track and get totals
"userUid" : "true",
"userUid4" : "true" }},
"1" : {
"Name" : "Foo",
"Source" : "Bar" },
"2" : {
"Name" : "Foo",
"Source" : "Bar",
"Favorites" : {
"userUid" : "true" }
}}
我的主要目标是根据用户之前的操作将 RecyclerView 项目设置为 "favorited"。我可以在点击时存储和删除这些数据,但我很难在正确的时间在正确的地方显示它。
我正在使用两个不同的节点来实现这一点:
"quotes" : {
"0" : {
"Name" : "Foo",
"Source" : "Bar" },
"1" : {
"Name" : "Foo",
"Source" : "Bar" },
"2" : {
"Name" : "Foo",
"Source" : "Bar" }
},
"favorites" : {
"blah@blah,com" : {
"uid0" : "0"
"uid1" : "2" }}}
基本上我想做的是: 在 RecyclerView 中显示所有报价,如果它们的 ID 显示在该唯一用户的收藏夹中,则将其可视化设置为收藏夹。我在此处包含了一些似乎对我不起作用的代码。
private void bindHeart(final ItemHeartCardBinding binding) {
if (inProgress) {
animateProgress(binding);
} else {
binding.favorite.setImageResource(R.drawable.favorite_state_list);
}
//Loop through users favorites to see if current item exists
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
String email = "";
if (user != null) {
email = user.getEmail();
email = email.replace(".", ",");
}
final DatabaseReference favoriteRef = mRootRef.child("favorites/" + email);
//quoteKeyStringResId is passed in here as each RecyclerView item is being created. It's the uid of each quote.
final Query queryRef = favoriteRef.orderByValue().startAt(quoteKeyStringResId).endAt(quoteKeyStringResId);
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue().toString().equals(quoteKeyStringResId)) {
binding.favorite.setChecked(checked);
} else {
binding.favorite.setChecked(!checked);
}
}
我仔细研究了 Firebase database samples 并找到了一种更好的数据建模方法。在同一节点中包含所有相关数据允许我同时查询所有数据,而不必担心保持 RecyclerView 项目的顺序相同。事情并没有这么浅薄,但我认为没关系:
"quotes" : {
"0" : {
"Name" : "Foo",
"Source" : "Bar",
//Moved this from a separate node into here
"Favorites" : {
//Each user will be listed if favorited, and removed if unfavorited, making it easy to track and get totals
"userUid" : "true",
"userUid4" : "true" }},
"1" : {
"Name" : "Foo",
"Source" : "Bar" },
"2" : {
"Name" : "Foo",
"Source" : "Bar",
"Favorites" : {
"userUid" : "true" }
}}