如何获取Firebase android中所有孩子的列表?

时间:2017-05-09 01:14:01

标签: android firebase firebase-realtime-database

我在Firebase中有以下模型

model

我正在尝试使用以下代码

从子节点帖子中获取所有值
  databasePostsReference = 
  FirebaseDatabase.getInstance().getReference().child("posts");

 final List<UserPostPOJO> list = Collections.EMPTY_LIST;

  databasePostsReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                Log.d(TAG, "onDataChange: entered list adding");
                UserPostPOJO post = snapshot.getValue(UserPostPOJO.class);
                list.add(post);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

但我收到错误java.lang.UnsupportedOperationException

在上面的场景中我知道错误是因为列表被声明为final并且我试图在onDataChangeMethod中赋值,所以抛出错误但是如果我没有声明列表变量final那么我不能从onDataChange中访问列表变量()方法如何解决这个问题

  public class UserPostPOJO {
    String uid;
    Double elevation;
    String uri;
    String feel;

    public UserPostPOJO() {
    }

    public UserPostPOJO(String uid, Double elevation, String uri, String feel) {
        this.uid = uid;
        this.elevation = elevation;
        this.uri = uri;
        this.feel = feel;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public Double getElevation() {
        return elevation;
    }

    public void setElevation(Double elevation) {
        this.elevation = elevation;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getFeel() {
        return feel;
    }

    public void setFeel(String feel) {
        this.feel = feel;
    }
    } 

错误日志:

  05-08 22:19:49.706 6421-6421/redeyes17.com.abhi.android.iamat                      
    D/recentfragment: onDataChange: entered list adding
   05-08 22:19:49.726 6421-6421/redeyes17.com.abhi.android.iamat 
    E/AndroidRuntime: FATAL EXCEPTION: main

   Process: redeyes17.com.abhi.android.iamat, PID: 6421

   java.lang.UnsupportedOperationException

   at java.util.AbstractList.add(AbstractList.java:404)

   at java.util.AbstractList.add(AbstractList.java:425)

   at redeyes17.com.abhi.android.iamat.UI.tabs
  .RecentFragment$1.onDataChange(RecentFragment.java:78)

  at com.google.android.gms.internal.zzbpx.zza(Unknown Source)

  at com.google.android.gms.internal.zzbqx.zzZT(Unknown Source)

  at com.google.android.gms.internal.zzbra$1.run(Unknown Source)

   at android.os.Handler.handleCallback(Handler.java:739)

  at android.os.Handler.dispatchMessage(Handler.java:95)

  at android.os.Looper.loop(Looper.java:158)

  at android.app.ActivityThread.main(ActivityThread.java:7230)

  at java.lang.reflect.Method.invoke(Native Method)

 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller
 .run(ZygoteInit.java:1230)

 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:1)

要获取所有这些值,请使用以下代码:

    databasePostsReference = FirebaseDatabase.getInstance().getReference().child("posts").child(postId);
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
           List<UserPostPOJO> list = new ArrayList<>();

           String elevation = (String) dataSnapshot.child("elevation").getValue();
           String feel = (String) dataSnapshot.child("feel").getValue();
           String uid = (String) dataSnapshot.child("uid").getValue();
           String uri = (String) dataSnapshot.child("uri").getValue();

          UserPostPOJO userPostPOJO = new UserPostPOJO();
          userPostPOJO.setElevation(elevation);
          userPostPOJO.setFeel(feel);
          userPostPOJO.setUid(uid);
          userPostPOJO.setUri(uri);

          list.add(userPostPOJO);
    }

          @Override
          public void onCancelled(DatabaseError databaseError) {}
};
databasePostsReference.addListenerForSingleValueEvent(eventListener);

其中postIdpush()方法生成的唯一ID。请注意,您list的声明必须在onDataChange()方法中,否则,您将获得空。

希望它有所帮助。