【问题标题】:Firebase retrieving multiple objects in multiple nodesFirebase 在多个节点中检索多个对象
【发布时间】:2018-06-29 10:23:20
【问题描述】:

我正在尝试从 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);
        }

    {
      "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);
                            }
                        }
                    }
                }

如果您需要我的其他东西,请告诉我。

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

【问题讨论】:

  • 你需要获取mLLocationData里面的值吗?
  • 是的,简而言之,我有 3 个模型用户、视频和位置。我需要这些价值观。特别是位置,因为我正在使用地图。 (你能告诉我关于节点顺序的图形错误的任何事情吗)

标签: java android firebase firebase-realtime-database


【解决方案1】:

如果你想用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

【讨论】:

  • 我已经有 3 个模型类。我需要从该数据库中检索所有内容。 mName、mVideoentry 和 mLocationData。不幸的是,这只适用于一个节点 eheh
  • 然后您可以将我在此处发布的值添加到它们并像我在这里一样引用,但是尝试单独执行此类并尝试执行此操作,它应该可以工作,然后您可以编辑您的代码,但出于理解目的,我认为它更清洁
  • 我已经为一个节点做了。我的问题是多个:(我们只是错过了一些非常小的东西
  • 看看这个链接,这应该可以解决你的问题quora.com/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多