[英]How to bypass unique ID and retrieve multiple auto generated child
I want to retrieve multiple child with an auto generated child(Estab_url child). 我想使用自动生成的子代(Estab_url子代)检索多个子代。 Here is my Code. 这是我的代码。 All i can retrieve is one child and its random 我只能找一个孩子,它是随机的
final DatabaseReference establishments = database.getReference("establishments");
establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.hasChildren()) {
// String value = dataSnapshot.getValue(String.class);
String sd = dataSnapshot.getKey();
Name.setText(sd);
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
If you're looking to read the URL value for each child: 如果您想读取每个孩子的URL值:
establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String url = dataSnapshot.child("url").getValue(String.class);
Name.setText(url);
}
If that is not what you're looking for, please update your question to rephrase the problem. 如果这不是您想要的,请更新您的问题以重新描述该问题。
Update 更新
If you'd like to catch all URLs in a list, you'd set up the list outside of the listener and then add each URL to it as it comes in: 如果您想捕获列表中的所有URL,则可以在侦听器之外设置列表,然后将每个URL添加进来:
List<String> urls = new LinkedList<String>();
establishments.child(s2).child("Estab_url").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String url = dataSnapshot.child("url").getValue(String.class);
urls.add(url);
// TODO: do something with the up-to-date list of URLs
}
But note that you still cannot use the list outside of the listener. 但是请注意,您仍然不能在侦听器之外使用列表。 Any code that needs the up-to-date content of the list, will need to be called/triggered from within (in this case) onChildAdded
. 需要列表的最新内容的任何代码都需要在onChildAdded
内部(在这种情况下)进行调用/触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.