简体   繁体   English

Android Firebase数据库如何从子节点检索所有数据

[英]android firebase database how to retrieve all data from child node

I've got an app where it retrieves a bunch of data from firebase but I cant seem to make it work. 我有一个应用程序,它可以从Firebase检索大量数据,但是我似乎无法使其工作。

database = FirebaseDatabase.getInstance();
    myRef = database.getReference().child("Sold").child("Item");

myListView = (ListView)findViewById(R.id.listView);
    final ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myArrayList);


    myListView.setAdapter(myArrayAdapter);

    myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            String value = dataSnapshot.getValue(String.class);
            myArrayList.add(value);
            myArrayAdapter.notifyDataSetChanged();
        }

It doesnt give me any error but it just closes my app. 它不会给我任何错误,但是会关闭我的应用程序。 Im trying to access URL/Sold/Item and pass the data in my listview 我试图访问URL /出售/项目并在我的列表视图中传递数据

点击这里查看我的firebase,谢谢

You're creating a reference with this: 您正在使用以下内容创建参考:

database = FirebaseDatabase.getInstance();
myRef = database.getReference().child("Sold").child("Item");

And then you add a ChildEventListener . 然后添加一个ChildEventListener That means that the DataSnapshot in your onChildAdded will be called with a snapshot of the DUMMY 1 node first, and then with a snapshot of the DUMMY 2 node. 这意味着onChildAdded中的DataSnapshot将首先使用DUMMY 1节点的快照调用,然后使用DUMMY 2节点的快照调用。

You're calling dataSnapshot.getValue(String.class) in onChildAdded . 你打电话dataSnapshot.getValue(String.class)onChildAdded But since DUMMY 1 and DUMMY 2 have multiple properties, they don't have a single string value. 但是,由于DUMMY 1DUMMY 2具有多个属性,因此它们没有单个字符串值。 So that call returns null , which is probably what's causing you problems. 因此该调用返回null ,这可能是导致您出现问题的原因。

If you want to add the DUMMY 1 , DUMMY 2 key itself to the adapter, you can call dataSnapshot.getKey() . 如果要将DUMMY 1DUMMY 2键本身添加到适配器,则可以调用dataSnapshot.getKey() Otherwise you can get the value of the specific child properties with dataSnapshot.child("Description").getValue(String.class) . 否则,您可以使用dataSnapshot.child("Description").getValue(String.class)获得特定子属性的值。

So: 所以:

myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        String key = dataSnapshot.getKey();
        String description = dataSnapshot.child("Description").getValue(String.class);
        Long quantity = dataSnapshot.child("Quantity").getValue(Long.class);
        myArrayList.add(key+": "+description+" ("+quantity+")");
        myArrayAdapter.notifyDataSetChanged();
    }

If you'd like to read the entire value into a class representing the item (a so-called POJO), the simplest class is: 如果您想将整个值读入表示项目的类(所谓的POJO),则最简单的类是:

public class Item {
  public String Description;
  public Long Quantity;
}

Note that the name (including the case) of the fields must exactly match the names of the properties in your database. 请注意,字段名称(包括大小写)必须与数据库中属性的名称完全匹配。 You can use the above class like this: 您可以像这样使用上面的类:

myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        String key = dataSnapshot.getKey();
        Item item = dataSnapshot.getValue(Item.class);
        myArrayList.add(key+": "+item.description+" ("+item.quantity+")");
        myArrayAdapter.notifyDataSetChanged();
    }

The problem is that you cannot convert the dataSnapshot you are receiving into a String. 问题是您无法将收到的dataSnapshot转换为String。

So it would be bettor to do like this: 因此,这样做是更好的选择:

 myRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    for (DataSnapshot dummys : dataSnapshot.getChildren()) {
                        String description = dummys.child("Description").getValue(String.class);
                        int Quantity = dummys.child("Quantity").getValue(Integer.class);
                        //Use your variables here
                    }                        
                }

Or if you want to create a object of Dummy: 或者,如果您要创建虚拟对象:

public class Dummy {

String Description;
Integer Quantity;

public Dummy(String description, Integer quantity) {
    Description = description;
    Quantity = quantity;
}

public String getDescription() {
    return Description;
}

public void setDescription(String description) {
    Description = description;
}

public Integer getQuantity() {
    return Quantity;
}

public void setQuantity(Integer quantity) {
    Quantity = quantity;
}

} }

myRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                    List<Dummy> dummyList = new ArrayList<>();
                    for (DataSnapshot dummys : dataSnapshot.getChildren()) {
                        dummyList.add(dummys.getValue(Dummy.class));
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Android Firebase 实时数据库中的节点检索所有数据? - How to retrieve all the data from a node in Android Firebase realtime database? 如何从Firebase / Android检索孩子的数据孩子 - How to retrieve data child of child from firebase / Android 如何在firebase数据库中检索特定孩子的数据 - android studio/java? - How to retrieve data for a specific child in firebase database - android studio/ java? 如何从 firebase 中的所有用户中检索特定的子数据 - How to retrieve a specific child data from all the users in firebase 如何使用 Android Studio 从 Firebase 实时数据库中的节点检索特定数据? - How to retrieve a specific data from a node in Firebase Realtime Database using Android Studio? 如何在 firebase 数据库中检索特定孩子的数据 - How to retrieve data of a particuler child in firebase database 如何从Firebase Android检索子数据[保持] - How to retrieve child data from firebase android [on hold] 如何从Firebase数据库检索数据并将所有数据传递到短信 - How to retrieve data from firebase database and pass all the data to sms 如何从Firebase数据库Android获取子节点 - How to get child node from Firebase database Android 如何从 Android Firebase 数据库 databaseRef-&gt;userID-&gt;uniqueKey 中检索所有数据 - How to retrieve all data from Android Firebase database databaseRef->userID->uniqueKey
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM