android firebase database how to retrieve all data from child node

放肆的年华 提交于 2020-01-06 05:16:12

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/52541406/android-firebase-database-how-to-retrieve-all-data-from-child-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!