问题检索Firebase的类似子元素 [英] Problem retrieve similar child element of firebase
本文介绍了问题检索Firebase的类似子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一次只检索日期和所有date元素,并将它们存储在firebase中的数组中,如下所示.
I want to retrieve only the date and all the date element at once and store them in array in the firebase as shown below.
我已使用以下代码进行检索,但未能成功.
i have used the following code to retrieve but failed to that.
public void showData(View view) {
final ArrayList<String> date = new ArrayList<>();
firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Date");
firebaseDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
date.add(dataSnapshot.getValue().toString());
display.setText(dataSnapshot.getValue().toString());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
请帮助找到解决方案.
推荐答案
正如@PeterHaddad在上一个答案中所说,访问已获取数据的顺序不正确.如果到目前为止没有其他帮助,您可以尝试这种方式.
As said by @PeterHaddad in the previous answer, the sequence to access the data you have taken is not right. You may try this way, if nothing else helped until yet.
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("Names").orderByChild("Date").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
date.add(ds.getValue(String.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//Also as you've told that code is not working, please tell, what exactly is not working
这篇关于问题检索Firebase的类似子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文