我正在尝试从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);
}
}
}
}
如果您需要我提供其他帮助,请告诉我。
我缺少一些东西,因为我可以看到生成的地图以及其中的所有值 感谢开发人员
答案 0 :(得分:0)
如果要使用mVideoEntry
的数据获取mLocationData
内部的所有值
执行此操作
创建一个新类,将其作为您的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());
}
});
通过此操作,您应该可以将所有mLocationData
内的所有mVideoEntry