从Firebase获取特定用户的分数和类别

时间:2018-07-28 16:21:44

标签: android json firebase firebase-realtime-database

我具有以下json结构:

"Question_Score" : {
    "anna_01" : {
      "categoryID" : "01",
      "categoryName" : "Intelligence",
      "question_Score" : "anna_01",
      "score" : "20",
      "user" : "anna"
    },
    "anna_03" : {
      "categoryID" : "03",
      "categoryName" : "Constitution",
      "question_Score" : "anna_03",
      "score" : "170",
      "user" : "anna"
    },
    "stelios_01" : {
      "categoryID" : "01",
      "categoryName" : "Intelligence",
      "question_Score" : "stelios_01",
      "score" : "230",
      "user" : "stelios"
    },
    "stelios_02" : {
      "categoryID" : "02",
      "categoryName" : "Widsom",
      "question_Score" : "stelios_02",
      "score" : "220",
      "user" : "stelios"
    },

我希望当前用户获得categoryID和得分。所以可能我需要检查是否等于currentuser == user,然后获取该用户的categoryID和得分。当我刚接触Firebase时,我发现它有点困难。

final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("Question_Score");

// Attach a listener to read the data at your profile reference
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Profile profile = dataSnapshot.getValue(Question_Score.class);

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.v("The read failed: " + databaseError.getCode());
    }
});

我从这里不知道如何获取CategoryId的值和得分来检查相等性。我会提供任何帮助!

1 个答案:

答案 0 :(得分:1)

如果我对您的理解正确,那么您正在尝试读取具有public interface IMonoid<T> : ISemiGroup<T> { T Identity { get; } } public interface IGroup<T> : IMonoid<T> { T Inverse(T a); } public interface IRing<T> { IGroup<T> AdditiveStructure { get; } IMonoid<T> MultiplicativeStructure { get; } } 特定值的public class ModularRing : Scaffolding.IRing<int> { public IGroup<int> AdditiveStructure { get; } = new ModularGroup(); public IMonoid<int> MultiplicativeStructure { get; } = new ModularMonoid(); } 子节点。您可以使用Question_Score属性上的Firebase数据库查询来做到这一点:

categoryID

这将打印categoryIDdatabase = FirebaseDatabase.getInstance(); questionScoreRef = database.getReference("Question_Score"); Query query = questionScoreRef.orderByChild("categoryID").equalTo("01"); query.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) { Log.i(TAG, childSnapshot.getKey()+": "+childSnapshot.child("user").getValue(String.class)); } } @Override public void onCancelled(DatabaseError databaseError) { // Getting Post failed, log a message Log.w(TAG, "loadPost:onCancelled", databaseError.toException()); // ... } }; questionScore.addValueEventListener(postListener); 的每个子节点的键和user值。