【发布时间】:2016-09-15 02:00:55
【问题描述】:
我正在尝试创建一个仅包含特定组项目的列表。例如,我自己在数据库控制台中创建/编写了一个“fruits”节点和一个“groups”节点(包括组的键)。
对于 Firebase 来说相对较新,因此我仍在尝试围绕扁平化数据结构展开工作。
JSON(显示“fruits”和“groups”)如下所示:
{
"fruits" : {
"apple" : {
"color" : "appleColorValueHere",
"groups" : {
"GroupABCKey" : true,
"GroupXYZKey" : true
},
"origin" : "appleOriginValueHere",
"size" : "appleSizeValueHere"
},
"orange" : {
"color" : "orangeColorValueHere",
"groups" : {
"GroupABCKey" : true,
"GroupXYZ" : true
},
"origin" : "orangeOriginValueHere",
"size" : "orangeSizeValueHere"
},
"strawberry" : {
"color" : "strawberryColorValueHere",
"groups" : {
"GroupJKLKey" : true
},
"origin" : "strawberryOriginValueHere",
"size" : "strawberrySizeValueHere"
}
},
"groups" : {
"GroupABCKey" : {
"members" : {
"apple" : true,
"orange" : true
},
"name" : "Group ABC Name Here"
},
"GroupJKLKey" : {
"members" : {
"strawberry" : true
},
"name" : "Group JKL Name Here"
},
"GroupXYZKey" : {
"members" : {
"apple" : true,
"orange" : true
},
"name" : "Group XYZ Name Here"
}
} ...
...
}
在应用程序本身中,用户可以创建一个列表,将整个 fruits 节点复制到他们新创建的列表中(请参阅下面的 JSON 以了解 user-list-items)。然后我使用FirebaseRecyclerAdapter显示这些项目
这是我如何做到这一点的代码:
private void writeNewFruitList(String userId, String username, String email, String title) {
final String key = databaseReference.child("fruit-lists").push().getKey();
UserLists userLists = new UserLists(userId, username, email, title);
HashMap<String, Object> updatedListToAddMap = new HashMap<>();
HashMap<String, Object> itemToAdd =
(HashMap<String, Object>) new ObjectMapper().convertValue(userLists, Map.class);
updatedListToAddMap.put("/fruit-lists/" + key, itemToAdd);
updatedListToAddMap.put("/user-fruit-lists/" + userId + "/" + key, itemToAdd);
databaseReference.updateChildren(updatedListToAddMap);
final DatabaseReference subDatabaseReference = FirebaseDatabase.getInstance().getReference();
subDatabaseReference.child("fruits").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
Log.d(TAG, child.getKey());
Log.d("fruitValues::", dataSnapshot.getValue().toString());
final String fruitKey= child.getKey();
FruitModel fruitModel = child.getValue(FruitModel.class);
Map<String, Object> nodeValues = fruitModel.toMap();
Map<String, Object> childUpdates = new HashMap<>();
String new_path = "/user-list-items/" + key + "/" + fruitKey+ "/";
childUpdates.put(new_path, nodeValues);
subDatabaseReference.updateChildren(childUpdates);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
}
问:如何使用组来实施此流程?例如,用户创建了一个列表,并且只希望新创建的列表包含GroupABCKey 的水果,但他们还想要这些水果的详细信息(大小、原产地等)。
Q2:编辑我的FruitModel.java 以说明群组的最佳方式是什么?
public class FruitModel {
public String size;
public String origin;
public String color;
public FruitModel() {
}
public FruitModel(String size, String origin, String color) {
this.size = size;
this.origin = origin;
this.color = color;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("size", size);
result.put("origin", origin);
result.put("color", color);
return result;
}
// getters & setters
}
最后,我认为user-list-items 节点如果操作正确会是什么样子?
listKey1forUserA 是我正确创建的(除了前面提到的映射组)。
listKey2forUserA 是我要完成的任务 - 用户希望仅使用“GroupJKLKey”中的项目创建和填充列表
"user-list-items" : {
"listKey1forUserA" : {
"apple" : {
"color" : "appleColorValueHere",
"groups" : {
"GroupABCKey" : true,
"GroupXYZ" : true
},
"origin" : "appleOriginValueHere",
"size" : "appleSizeValueHere"
},
"orange" : {
"color" : "orangeColorValueHere",
"groups" : {
"GroupABCKey" : true,
"GroupXYZKey" : true
},
"origin" : "orangeOriginValueHere",
"size" : "orangeSizeValueHere"
},
"strawberry" : {
"color" : "strawberryColorValueHere",
"groups" : {
"GroupJKLKey" : true
},
"origin" : "strawberryOriginValueHere",
"size" : "strawberrySizeValueHere"
}
},
"listKey2forUserA" : {
"strawberry" : {
"color" : "strawberryColorValueHere",
"groups" : {
"GroupJKLKey" : true
},
"origin" : "strawberryOriginValueHere",
"size" : "strawberrySizeValueHere"
}
}
}
这是解决此问题的正确方法吗?任何帮助或正确方向的观点都会很棒。
【问题讨论】:
标签: android firebase firebase-realtime-database