java - Firebase在多个节点中检索多个对象

标签 java android firebase firebase-realtime-database

我正在尝试从 android 中的 firebase 实时数据库读取数据 (出于某种原因,FireBase 对位置数据进行排序,然后对 VideoEntry 进行排序,但我认为无论如何它都是正确的......根据生成的 json)

如果您需要,这就是我添加节点的方式:

    submitVideoButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String locationName = locationInput.getText().toString();
                    double lat = Double.parseDouble(latInput.getText().toString());
                    double lon = Double.parseDouble(longInput.getText().toString());

                    String userid = mFirebaseUser.getEmail().substring(0, mFirebaseUser.getEmail().indexOf("@"));
                    String cleanUserId = userid.replaceAll("\\W", "");
                    updateUserInfo(cleanUserId, mFirebaseUser.getEmail(), "nsaofdhiqo3", locationName, lat, lon);
                }
            });

        }

        private void updateUserInfo(String userId, String email, String videoId, String locName, double lat, double lon) {
            LocationData location = new LocationData(locName,lat,lon);
            VideoEntry videoEntry = new VideoEntry(videoId,location);
            UserData user = new UserData(email, videoEntry);

            mDatabaseRef.child("users").child(userId).setValue(user);
        }

enter image description here

    {
      "users" : {
        "valentinogiuseppe00" : {
          "mName" : "valentino.giuseppe00@gmail.com",
          "mVideoEntry" : {
            "mLocationData" : {
              "mLatitude" : 58,
              "mLongitude" : 10,
              "mName" : "locationName"
            },
            "mVideoId" : "nsaofdhiqo3"
          }
        }
      }
    }

这是我到目前为止尝试过的:

    databaseRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    double lat = 0.0, lon = 0.0;
                    String locName = null;
                    String fVideoId = null, videoName = null;
                    String userName = null;

                    //TODO Need to re-structure the  to add markers
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                        Log.d(TAG, "userSnapShot children #:" + userSnapshot.getChildrenCount());
                        userName = userSnapshot.child(cleanUserId).getValue(String.class);
                        for (DataSnapshot videoSnapShot : dataSnapshot.getChildren()) {
                            Log.d(TAG, "videoSnapShot children #:" + videoSnapShot.getChildrenCount());
                            fVideoId = videoSnapShot.child("videoId").getValue(String.class);
    //                        videoName = videoSnapShot.child("").getValue(String.class);
                            for (DataSnapshot locationSnapShot : dataSnapshot.getChildren()) {
                                Log.d(TAG, "locationSnapShot children #:" + (locationSnapShot.getChildrenCount()));
                                lat = locationSnapShot.child("mLatitude").getValue(Double.class);
                                lon = locationSnapShot.child("mLongitude").getValue(Double.class);
                            }
                        }
                    }
                }

如果您需要我提供其他信息,请告诉我。

我错过了一些东西,因为我可以看到生成的 map 以及其中的所有值 感谢各位开发者

最佳答案

如果你想通过mLocationData的数据获取mVideoEntry内的所有值

这样做

创建一个新类作为您的 pojo 对象,称为 userPojo.class

public class UserPojo {


   private long mLatitude;
   private long mLongitude;
   private String mName;

    public long getmLatitude() {
        return mLatitude;
    }

    public void setmLatitude(long mLatitude) {
        this.mLatitude = mLatitude;
    }

    public long getmLongitude() {
        return mLongitude;
    }

    public void setmLongitude(long mLongitude) {
        this.mLongitude = mLongitude;
    }

    public String getmName() {
        return mName;
    }

    public void setmName(String mName) {
        this.mName = mName;
    }




}

然后只需在您的引用中迭代 mVideoEntry 子级并获取您的值

databaseRef.child("users").child("valentinogiuseppe00").child("mVideoEntry").addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {

    for(DataSnapshot snapShot : dataSnapshot.getChildren()){
    UserPojo user= snapShot.getValue(UserPojo.class);
    //Getting your values
     long longitude = user.getmLongitude();
     long latitude = user.getmLatitude();
     String name = user.getmName();
     Log.e("Data: " , "" + latitude + "" + longitude + "" + name);
     }
  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    System.out.println("The read failed: " + databaseError.getCode());
  }
});

有了这个,您应该能够在每个 mVideoEntry 中获取所有 mLocationData

关于java - Firebase在多个节点中检索多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51092667/

相关文章:

java - 哪个 Java API 与 JavaCV 一起使用? org.bytedeco.opencv.opencv_core 还是 org.opencv.core?

android - 使用 libgdx 在 Android/iOS 上开发 2D 游戏需要 Mac?

javascript - vue 生产模式下的 "localhost"内部静态资源 url

android - 是否可以根据名称实例化布局?

firebase - 更新 cloud firestore : The operator '[]' isn't defined for the type 'Object' . 后尝试定义操作符 '[]'

ios - 为什么这个完成处理程序会跳过 for 循环

java - 在 Java 中使用 Double 值序列化和反序列化数据

java - Jinput - 获取鼠标位置

java - java : too many questions? 中的内存映射文件

android - 为什么 Android Studio "Could not find kotlin-compiler-27.0.1.jar"?