我搜索了很多帖子以找到解决方案,但我找不到一个。 我有两个类食谱类:
public class Recepta {
private String nom;
private List<Ingredient> ingredients;
private List<String> pasos;
public Recepta() {
}
public Recepta(String nom,List<Ingredient> ingredients, List<String> pasos) {
this.nom = nom;
this.ingredients = ingredients;
this.pasos = pasos;
}
//Getter and setters
}
和我的成分类:
public class Ingredient {
private String nom;
private String quantitat;
public Ingredient(){
}
public Ingredient(String nom,String quantitat){
this.nom = nom;
this.quantitat = quantitat;
}
//Getter and setters
}
我的DB的结构:
3ER2U0QSDTMHRdKA0eO231UCQeY2
8eJaHRU9f5ZrVogqhx0Hj1iFcgG2
-jdhd
-ingredients
-0
-nom: "bdhd"
-quantitat: "b,bzh"
-nom:"jdhd"
-pasos
-0: "djdjjd"
-jdjd
-jdjdvshs
AfcGeBzvdLRq19J3kRk3CVg30np2
fk39ClAVaQNFQCONU9Agp0KfUUf1addclose
如您所见,“pasos”是一个字符串列表,“成分”是一个成分列表。
我无法找到的是如何从数据库中检索这些列表,因为我想获得一个Recepta列表。 我已经尝试了很多东西,但都没错,因为它没有找回任何列表。
这就是我尝试过的,但我知道这不起作用,因为Firebase DB不会将列表存储为列表
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final DatabaseReference listReference = FirebaseDatabase.getInstance().getReference(user.getUid());
listReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(final DataSnapshot listSnapshot: dataSnapshot.getChildren()){
receptas.add(listSnapshot.getValue(Recepta.class));
}
listReceptaAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我也知道FB DB使用HashMaps但我不知道如何检索它们或从哈希映射传递到列表而不必遍历hashmap的每个位置
那我该怎么办?提前谢谢
答案 0 :(得分:0)
我认为以下示例有助于将两个数据集成到firebase数据库的一个列表中
public static ArrayList<MuteModel> muteModelList;
public void updateMuteListServer(){
DatabaseReference frndMuteRef = FirebaseDatabase.getInstance().getReference().child("users/" + getFirebaseUser().getUid().trim() + "/friendlists");
Query queryRef = frndMuteRef.orderByChild(MUTE).equalTo(YES);
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
GenericTypeIndicator<HashMap<String,MuteModel>> t= new GenericTypeIndicator<HashMap<String,MuteModel>>() { };
HashMap<String,MuteModel> hashMap= (HashMap<String,MuteModel>)dataSnapshot.getValue(t);
if(hashMap!=null) {
muteModelList = new ArrayList<MuteModel>(hashMap.values());
for (MuteModel muteModel: muteModelList){
muteModel.setIsgroup(NO);
}
}else{
muteModelList = new ArrayList<MuteModel>();
}
findGroupMuteList();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void findGroupMuteList() {
DatabaseReference groupMuteRef = FirebaseDatabase.getInstance().getReference().child("users/" + getFirebaseUser().getUid().trim() + "/"+GROUPS);
Query queryRef = groupMuteRef.orderByChild(MUTE).equalTo(YES);
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
GenericTypeIndicator<HashMap<String,MuteModel>> t= new GenericTypeIndicator<HashMap<String,MuteModel>>() { };
HashMap<String,MuteModel> hashMap= (HashMap<String,MuteModel>)dataSnapshot.getValue(t);
ArrayList<MuteModel> properties;
if(hashMap!=null) {
properties = new ArrayList<MuteModel>(hashMap.values());
for (MuteModel muteModel:properties){
muteModel.setIsgroup(YES);
}
}else{
properties = new ArrayList<MuteModel>();
}
MyChat.muteModelList.addAll(properties);
Gson gson = new Gson();
String strMuteList = gson.toJson(MyChat.muteModelList);
editSharedPref(MUTELIST,strMuteList);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
在您的情况下,我认为成分的通用适配器将如下所示
GenericTypeIndicator<ArrayList<Ingredients>>
&安培;如果是pasos,你可以这样做
GenericTypeIndicator<ArrayList<Strings>>