如何从Firebase检索子数据?

huangapple go评论132阅读模式
英文:

How to retrieve the child data from Firebase?

问题

以下是翻译好的部分:

private void loadAddress() {
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
    ref.orderByChild("uid").equalTo(firebaseAuth.getUid())
        .addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()){
                    String name = "" + ds.child("name").getValue();
                    String lat = "" + ds.child("name2").getValue();
                    addresstv.setText(name);
                }
            }

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

            }
        });
}
英文:

My database looks as follows, in here I want to retrieve the data inside the mylocation child (see the attached image here). But it is not working.
How to solve that?

My current code looks as this,

private void loadAddress() {
        DatabaseReference ref= FirebaseDatabase.getInstance().getReference("Users");
        ref.orderByChild("uid").equalTo(firebaseAuth.getUid())
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot ds: dataSnapshot.getChildren()){
                            String name=""+ds.child("name").getValue();

                            String lat=""+ds.child("name2").getValue();

                             addresstv.setText(name);

                        }
                    }

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

                    }
                });



    }

答案1

得分: 1

获取mylocation下的数据,您需要显式调用该子项,就像以下代码中所示:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("mylocation").child("name2").getValue(String.class);
        double latitude = dataSnapshot.child("mylocation").child("latitude").getValue(Double.class);
        double longitude = dataSnapshot.child("mylocation").child("longitude").getValue(Double.class);
        Log.d("TAG", name + "/" + latitude + "/" + longitude);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); // 不要忽略错误!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

日志中的结果将是:

5555/79.8571577/6.9448882
英文:

To get the data under mylocation, you use an explicit call to that child, like in the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
	@Override
	public void onDataChange(DataSnapshot dataSnapshot) {
		String name = dataSnapshot.child("mylocation").child("name2").getValue(String.class);
		double latitude = dataSnapshot.child("mylocation").child("latitude").getValue(Double.class);
		double longitude = dataSnapshot.child("mylocation").child("longitude").getValue(Double.class);
		Log.d("TAG", name + "/" + latitude + "/" + longitude);
	}

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

The result in the logcat will be:

5555/79.8571577/6.9448882

huangapple
  • 本文由 发表于 2020年7月23日 15:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63049057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定