Android Firebase数据库如何从子节点检索所有数据 [英] android firebase database how to retrieve all data from child node
问题描述
我有一个应用程序,它可以从Firebase检索大量数据,但是我似乎无法使其正常工作.
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();
}
它不会给我任何错误,但是会关闭我的应用程序. 我试图访问URL/Sold/Item并在listview中传递数据
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
推荐答案
您正在使用以下内容创建引用:
You're creating a reference with this:
database = FirebaseDatabase.getInstance();
myRef = database.getReference().child("Sold").child("Item");
然后添加ChildEventListener
.这意味着,将首先使用DUMMY 1
节点的快照调用onChildAdded
中的DataSnapshot
,然后使用DUMMY 2
节点的快照进行调用.
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.
您正在onChildAdded
中呼叫dataSnapshot.getValue(String.class)
.但是,由于DUMMY 1
和DUMMY 2
具有多个属性,因此它们没有单个字符串值.因此该调用会返回null
,这可能是导致您出现问题的原因.
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.
如果要将DUMMY 1
,DUMMY 2
键本身添加到适配器,则可以调用dataSnapshot.getKey()
.否则,您可以使用dataSnapshot.child("Description").getValue(String.class)
获取特定子属性的值.
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)
.
所以:
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();
}
如果您想将整个值读入表示该项目的类(所谓的POJO),则最简单的类是:
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();
}
这篇关于Android Firebase数据库如何从子节点检索所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!