如何遍历DataSnapshot? [英] How to iterate over a DataSnapshot?

查看:69
本文介绍了如何遍历DataSnapshot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历contact节点的子节点. .

I'm trying to iterate over the children of the contacts node. .

但是我的ListArray contacts仅部分填充.前两个条目按预期填充阵列(Logcat的输出),但是第三个条目无法填充阵列.这是MainActivity.java:

However my ListArray contacts only partially populates. The first two entries populate the array as expected (output from Logcat) but the 3rd entry fails to populate the array. Here is the MainActivity.java:

public class MainActivity extends AppCompatActivity {
TextView stringTextView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    stringTextView = (TextView)findViewById(R.id.textView2);

    FirebaseDatabase firebaseDatabase= FirebaseDatabase.getInstance().getInstance();
    DatabaseReference dbRef= firebaseDatabase.getReference("County/Dublin");

    dbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            DataSnapshot contactSnapshot = dataSnapshot.child("contacts");
            Iterable<DataSnapshot> contactChildren = contactSnapshot.getChildren();
            ArrayList<Contact> contacts= new ArrayList<>();

            for (DataSnapshot contact : contactChildren) {
                Contact c = contact.getValue(Contact.class);
                Log.d("contact:: ", c.name + " " + c.number);
                contacts.add(c);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
}
}

和模型类Contact.java:

public class Contact {
public String name;
public String number;

public Contact() {
}


public Contact(String name, String number) {
    this.name = name;
    this.number = number;
}
}

为什么我的ListArray没有完全填充?

Why is my ListArray not completely populating?

更新

这是我对建议的代码示例所做的工作:

Here is what I've done with the suggested code example:

@Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<String> names= new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class);
                names.add(name);
                String number = ds.child("number").getValue(String.class);
                Log.d("TAG", name);
            }
            for(int i=0; i < names.size(); i++){

                stringTextView.setText(stringTextView.getText() + names
                        .get(i) + " , ");
            }
        }

阵列仍未填充,并且屏幕上也没有显示.

Array still not populating and no display to the screen.

推荐答案

请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("County").child("Dublin").child("contacts");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ArrayList<String> names= new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            names.add(name);
            String number = ds.child("number").getValue(String.class);
            Log.d("TAG", name + " / " + number);
        }
        for(String name : names) {
            TextView stringTextView = (TextView) findViewById(R.id.string_text_view);
            stringTextView.setText(stringTextView.getText().toString() + name + " , ");
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(eventListener);

您的输出将是:

ger / 0213
paddy / 3345556
jeff / 55555

因此,有了这些数据,您可以将其添加到ArrayList中,进行显示,并随心所欲地进行处理.

So, having this data, you can add it to an ArrayList, display it, do what you want with it.

这篇关于如何遍历DataSnapshot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆