简体   繁体   English

在 Firebase 上显示来自实时数据库的数据

[英]Displaying data from Real-time database on Firebase

I am trying to display data in my database but I'm having a problem doing so.我正在尝试在我的数据库中显示数据,但这样做时遇到了问题。 The data I'm trying to display is saved as "total" and it is saved as an int.我试图显示的数据保存为“总计”,并保存为 int。 I have tried saving it as a string and double but still no luck.我尝试将其保存为字符串和双精度,但仍然没有运气。 Ideally, I would like to display the total then display a message if the total is over the value 9. This is what I have so far.理想情况下,我想显示总数,然后在总数超过 9 时显示一条消息。这就是我目前所拥有的。

public class Results extends AppCompatActivity {
    private DatabaseReference mDatabase;
    private FirebaseAuth fAuth;
    private Button btn;
    private TextView userR;
    private  TextView num;

private FirebaseUser currentUser;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);
    userR = (TextView) findViewById(R.id.userResults);

    btn = (Button) findViewById(R.id.generate);
    fAuth = FirebaseAuth.getInstance();
    currentUser = fAuth.getCurrentUser();

    mDatabase = FirebaseDatabase.getInstance().getReference().child("FAQ");
}

@Override
public void onStart() {
    super.onStart();

    ValueEventListener scoreListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            faqinputs FAQInputs = dataSnapshot.getValue(faqinputs.class);
            userR.setText(FAQInputs.getTotal());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };

    mDatabase.addValueEventListener(scoreListener);
}

} }

"total" is saved in a "FAQ" node which is saved under a "User" “total”保存在“FAQ”节点中,该节点保存在“User”下

The data structure is as follows: This is a screenshot some data stored under set nodes数据结构如下:这是set节点下存储的一些数据的截图

The below data is the code I used to create the FAQ node.下面的数据是我用来创建FAQ节点的代码。

  private void sendScoreData() {
        String currentUserID = fAuth.getUid();
        faqinputs FAQInputs = new faqinputs(questionOne, questionTwo, questionThree, questionFour, questionFive, questionSix, questionSeven, questionEight, questionNine, questionTen, total);
        mDatabase.child("Users").child(currentUserID).push().child("FAQ").setValue(FAQInputs);


    }

            }

To get the value of the total property, you have to add all node names in your reference, like in the following lines of code:要获取total属性的值,您必须在引用中添加所有节点名称,如以下代码行:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference totalRef = rootRef.child("Users").child(uid).child("-M4YRj8nbobkuKurbHqC").child("FAQ").child("total");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int total = dataSnapshot.getValue(Integer.class);
        Log.d("TAG", "total: " + total);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
    }
};
totalRef.addListenerForSingleValueEvent(valueEventListener);

The result in the logcat will be: logcat 中的结果将是:

total: 15

I see the total to be a number, however, if you save it as a String, use this line:我看到总数是一个数字,但是,如果将其保存为字符串,请使用以下行:

String total = dataSnapshot.getValue(String.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Firebase 读取实时数据 - Read real-time data from Firebase 我无法在 recyclerView 上显示来自我的 firebase 实时数据库的数据 - I can't show data from my firebase real-time database on a recyclerView Firebase 实时数据库延迟 - Firebase real-time database latency firebase 实时数据库到列表视图? - firebase Real-Time Database to List View? Firebase 实时数据库规则 - Firebase real-time database rules 如何将列表视图中我从 Firebase 实时数据库中调用的数据移动到 android 工作室中的另一个活动? - How to move the data that I have called from the Firebase real-time database, in a listview to another activity in android studio? 插入数据到firebase实时数据库后看不到数据 - Can't see the data after I inserted the data to firebase real-time database 应用更新 firebase 实时数据库中的数据,但在 recyclerview 中不显示任何内容 - App updates data in firebase real-time database but doesn't show anything in recyclerview 如何从 Firebase 实时数据库的推送方法中删除自动生成的项目 - how to delete the auto generated item from push method from Firebase Real-time database 如何从Firebase实时数据库中具有不同键的节点读取相同的值? - How to read same value from nodes with different keys in Firebase real-time database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM