简体   繁体   中英

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.

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

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

You're creating a reference with this:

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

And then you add a 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.

You're calling dataSnapshot.getValue(String.class) in onChildAdded . But since DUMMY 1 and DUMMY 2 have multiple properties, they don't have a single string value. So that call returns null , which is probably what's causing you problems.

If you want to add the DUMMY 1 , DUMMY 2 key itself to the adapter, you can call dataSnapshot.getKey() . Otherwise you can get the value of the specific child properties with 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:

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.

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));
                    }
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM