2017-11-25 240 views
0

我试图获取Firebase数据库中各节的名称并将其添加到arraylist,但它只返回null。在5节点深度系统中检索第3个节点

Firebase Node
这是我的代码:

mSectionReference = database.getReference().child("/apartments/").child("/B3/").child("/sections/"); 
    ValueEventListener sectionListner = new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      Log.d("sections", "onDataChange"); 

      if (dataSnapshot.exists()) { 
       Log.d("sections", "snapshot exists"); 

       for (DataSnapshot sectionSnapshot : dataSnapshot.getChildren()) { 
        if (sectionSnapshot != null) { 
         Section section = sectionSnapshot.getValue(Section.class); 
         Log.d("sections", "Section created: " + section.getName()); 

        } else 
         Log.d("sections", "sections null"); 
       } 
      } 
     } 

     @Override 
     public void onCancelled(DatabaseError error) { 
      Log.w("failedSnap", "Failed to read value.", error.toException()); 
     } 
    }; 
    mSectionReference.addValueEventListener(sectionListner); 

这是logcat的结果:

onDataChange 
snapshot exists 
Section created: null 
Section created: null 
+1

include部分对象实现。 –

回答

0

SOLUTION:

我发现,即使有在路径中的元素,这并不意味着有一个“对象”在那里。

我实现了一个新的HashMap,节省的部分作为重点和值的名称,使结构是这样的: firebase database strcture

0

为了解决这个问题,请更改这行代码:

mSectionReference = database.getReference().child("/apartments/").child("/B3/").child("/sections/"); 

mSectionReference = database.getReference().child("apartments").child("B3").child("sections"); 

当你通过孩子的名字作为参数不需要的斜线。