Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android firebase数据库如何从子节点检索所有数据_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java android firebase数据库如何从子节点检索所有数据

Java android firebase数据库如何从子节点检索所有数据,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我有一个应用程序,它可以从firebase检索大量数据,但我似乎无法让它工作 database = FirebaseDatabase.getInstance(); myRef = database.getReference().child("Sold").child("Item"); myListView = (ListView)findViewById(R.id.listView); final ArrayAdapter<String> myArrayAdapte

我有一个应用程序,它可以从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();
        }
database=FirebaseDatabase.getInstance();
myRef=database.getReference().child(“已售出”).child(“项目”);
myListView=(ListView)findViewById(R.id.ListView);
最终阵列适配器myArrayAdapter=新阵列适配器(此,
android.R.layout.simple_list_item_1,myArrayList);
setAdapter(myArrayAdapter);
myRef.addChildEventListener(新的ChildEventListener(){
@凌驾
公共void onChildaded(@NonNull DataSnapshot DataSnapshot,@null字符串s){
String value=dataSnapshot.getValue(String.class);
myArrayList.add(值);
myArrayAdapter.notifyDataSetChanged();
}
它没有给我任何错误,但它只是关闭了我的应用程序。 我正在尝试访问URL/sell/Item并在我的listview中传递数据


您正在使用以下内容创建引用:

database = FirebaseDatabase.getInstance();
myRef = database.getReference().child("Sold").child("Item");
然后添加一个
ChildEventListener
。这意味着
onchildaded
中的
DataSnapshot
将首先使用
DUMMY 1
节点的快照调用,然后使用
DUMMY 2
节点的快照调用

您正在
onchildaded
中调用
dataSnapshot.getValue(String.class)
。但是由于
DUMMY 1
DUMMY 2
具有多个属性,它们没有单个字符串值。因此,该调用返回
null
,这可能是导致您出现问题的原因

如果要将
DUMMY 1
DUMMY 2
键本身添加到适配器,可以调用
dataSnapshot.getKey()
。否则可以使用
dataSnapshot.child(“Description”).getValue(String.class)
获取特定子属性的值

因此:


如果要将整个值读入表示该项的类(所谓的POJO),最简单的类是:

public class Item {
  public String Description;
  public Long Quantity;
}
请注意,字段的名称(包括大小写)必须与数据库中属性的名称完全匹配。您可以像这样使用上述类:

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

问题是无法将接收到的dataSnapshot转换为字符串

所以最好是这样做:

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();
    }
 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
                    }                        
                }
或者,如果要创建虚拟对象:

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(新的ChildEventListener(){
@凌驾
公共void onChildaded(@NonNull DataSnapshot DataSnapshot,@null字符串s){
List dummyList=new ArrayList();
对于(DataSnapshot dummys:DataSnapshot.getChildren()){
add(dummys.getValue(Dummy.class));
}
}

非常感谢,先生!如果我制作了一个包含描述和数量的对象,我该怎么做呢,先生?我添加了一个例子。搜索
firebase
android
pojo
应该会提供更多问题来收集信息。非常感谢,先生!我还是firebase和android的新手。这帮助很大!谢谢你!!谢谢你,先生!我刚刚对弗兰克爵士发表了评论,并要求更新页面和中提琴!回答!谢谢你,好先生!