【问题标题】:Retrieve specific data from firebase database从 firebase 数据库中检索特定数据
【发布时间】:2018-05-05 11:15:04
【问题描述】:

我正在尝试从 firebase 数据库中检索数据。这是我的数据库的结构

我想获取引用XqU1a6jrvKSLf4ulhI3LkOhChE92 和子ahmad 的数据并在textView 中显示bikingTime,但我得到0。这段代码我写在MainActivity

child mychild=new child();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirebaseDatabase database= FirebaseDatabase.getInstance();

    String name="ahmad";
    DatabaseReference myRef = database.getReference("XqU1a6jrvKSLf4ulhI3LkOhChE92").child(name);

    ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {



            mychild = dataSnapshot.getValue(child.class);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    myRef.addValueEventListener(listener);
    test = (TextView) findViewById(R.id.test);
    test.setText(String.valueOf(mychild.getBikingTime()));
}

这是child 类:

public class child {
public int bikingTime=0;
public int noOfKicks=0;
public int noOfJumps=0;


public child() {
}

public child(int bikingTime, int noOfKicks, int noOfJumps) {
    this.bikingTime = bikingTime;
    this.noOfKicks = noOfKicks;
    this.noOfJumps = noOfJumps;
}

public int getBikingTime() {
    return bikingTime;
}

public void setBikingTime(int bikingTime) {
    this.bikingTime = bikingTime;
}

public int getNoOfKicks() {
    return noOfKicks;
}

public void setNoOfKicks(int noOfKicks) {
    this.noOfKicks = noOfKicks;
}

public int getNoOfJumps() {
    return noOfJumps;
}

public void setNoOfJumps(int noOfJumps) {
    this.noOfJumps = noOfJumps;
}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    更改这些东西将根节点添加到查询中..

            DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("child-activity-kit").child("XqU1a6jrvKSLf4ulhI3LkOhChE92").child(name);
    
    
      DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
        Query query=myRef.child("XqU1a6jrvKSLf4ulhI3LkOhChE92").child(name);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                mychild = dataSnapshot.getValue(child.class);
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    

    【讨论】:

    • 你给正确的根节点和子节点键。
    • 对不起,我没听懂
    • 这意味着在firebase中检查根节点名称和子节点名称是否相同。
    • 去掉根节点试试看。
    • 放置调试器并检查它是否进入onDatachange方法。
    【解决方案2】:

    您可以按如下方式更改您的代码。它应该可以工作。

    child mychild=new child();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            test = (TextView) findViewById(R.id.test);
    
            FirebaseDatabase database= FirebaseDatabase.getInstance();
    
            String name="ahmad";
            DatabaseReference myRef = database.getReference().child("XqU1a6jrvKSLf4ulhI3LkOhChE92").child(name);
    
            ValueEventListener listener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
    
                    mychild = dataSnapshot.getValue(child.class);
                    test.setText(String.valueOf(mychild.getBikingTime()));
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            };
            myRef.addValueEventListener(listener);
    
    
        }
    

    【讨论】:

    • 它从 firebase 读取数据,但不会将其保存在对象“child”的字段中。一旦我们退出 onDataChange 函数,所有变量都归零
    • 你能帮忙看看如何保存数据
    • 嘿伙计,很抱歉回复晚了,因为我没有收到有关您的 cmets 的通知。当您使用 Firebase 实时数据库时,要了解如何将数据保存到数据库,您可以阅读 Firebase 网站firebase.google.com/docs/database/android/read-and-write 的官方文档。如果您遇到困难,请发布一个新问题,人们会帮助您。我对 Firebase 实时数据库很有经验,因此您可以在此处发布问题链接作为评论通知我。如果我知道答案,我会帮助你。不过我还在学习。
    • 顺便说一句,如果我上面的回答是正确的或对你有帮助,请标记为答案或投票,这样我可以获得更多的声誉。谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-02-04
    • 2019-02-07
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2018-12-09
    • 2021-08-22
    相关资源
    最近更新 更多