简体   繁体   English

firebase 实时数据库到列表视图?

[英]firebase Real-Time Database to List View?

数据库

Is there a way to retrieve all the child nodes to a textView?, i wanted to create a users log where you can tract what kind of transactions users have made, but i can't figure out how to get the nodes, and its list of children.有没有办法将所有子节点检索到 textView?,我想创建一个用户日志,您可以在其中跟踪用户所做的交易类型,但我不知道如何获取节点及其列表孩子的。

the code so far:到目前为止的代码:

database = FirebaseDatabase.getInstance();
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    reference = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Logs");
btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot datasnapshot) {
                    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
                    String logs = datasnapshot.child(uid).child("Logs").getValue().toString();

                    tv1.setText(logs);
                }

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

                }
            });

You're attaching a ValueEventListener to path /Users/$uid/Logs in your database, so the datasnapshot in your onDataChange contains the information for that node and any data under it.您将ValueEventListener附加到数据库中的路径/Users/$uid/Logs ,因此datasnapshot中的onDataChange包含该节点的信息及其下的任何数据。

When you then try to get the value of datasnapshot.child(uid).child("Logs") from that data snapshot, there is not child $uid/Logs in datasnapshot , so the value is going to be null .然后,当您尝试从该数据快照中获取datasnapshot.child(uid).child("Logs")的值时, null中没有子$uid/Logs ,因此该值将是datasnapshot


To get the value for all the logs, you can要获取所有日志的值,您可以

datasnapshot.getValue()

This will return a Map<String, Object> that you can then loop over.这将返回一个Map<String, Object> ,然后您可以对其进行循环。


Alternatively, you can loop over all the logs by date, and then by time, and print the value of the Date property for each, that'd be something like:或者,您可以按日期循环遍历所有日志,然后按时间循环,并为每个日志打印Date属性的值,如下所示:

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot datasnapshot) {
        for (DataSnapshot daySnapshot: datasnapshot.getChildren()) {
            Log.d("Firebase", dateSnapshot.getKey());
            for (DataSnapshot timeSnapshot: daySnapshot.getChildren()) {
                Log.d("Firebase", timeSnapshot.getKey());
                Log.d("Firebase", timeSnapshot.child("Date").getValue(String.class));
            }
        }
    }
    ...

To learn how to then show this in a list view or a recycler view, I recommend searching for that: https://www.google.com/search?q=how+to+show+data+from+firebase+in+a+list+view要了解如何在列表视图或回收器视图中显示此内容,我建议搜索: https://www.google.com/search?q=how+to+show+data+from+firebase+in+一个+列表+视图

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM