【发布时间】:2019-04-07 00:16:35
【问题描述】:
我正在使用 GeoFire 来获取特定半径内的所有用户。当我 addGeoQueryEventListener & 然后使用 for 循环来获取所有用户 ID,而不是返回 UserID 时,它会进入另一个更深的嵌套,返回 g 和 1。
private int radius = 40;
private Boolean userFound = false;
private String userLocationID;
private ArrayList<String> mUserIDLocation;
final UserLocation userLocation = new UserLocation();
public void getUserLocation() {
mUserIDLocation = new ArrayList<String>();
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
fusedLocationClient.getLastLocation().addOnSuccessListener((Activity) MainActivity.this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
//Toast.makeText(this, "UserLocation " + location.toString(), Toast.LENGTH_SHORT ).show();
//final Location userLocation = location;
Log.d(TAG, "onSuccess: UserLocation" + location);
Log.d(TAG, "onSuccess: UserLocation Latitude " + location.getLatitude());
String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
final GeoFire geoFire = new GeoFire(mRef.child("user_location"));
geoFire.setLocation(user_id, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
}
});
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(location.getLatitude(), location.getLongitude()), radius);
geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
@Override
public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
Log.d(TAG, "onDataEntered: DataSnapshot Key " + dataSnapshot);
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
mFollowing.add(snapshot.getKey());
}
getPost();
Log.d(TAG, "onDataEntered: mFollowing " + mFollowing);
Log.d(TAG, "onDataEntered: mFollowing size " + mFollowing.size());
}
@Override
public void onDataExited(DataSnapshot dataSnapshot) {
}
@Override
public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {
}
@Override
public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
}
});
}
数据库快照日志:
onDataEntered: DataSnapshot Key DataSnapshot { key = xjfXxJ3spuPNywtHyqg5rNnlIMD3, value = {.priority=c295tcm4kd, l={0=48.435, 1=-122.084}, g=c295tcm4kd} }
onDataEntered: DataSnapshot Key DataSnapshot { key = TMOb5NL8igZovGkiZdVcl3UQmxV2, value = {.priority=c295myvnsd, l={0=48.4319983, 1=-122.084}, g=c295myvnsd} }
记录添加到 mFollowing ArrayList 的内容: onDataEntered: mFollowing [g, l] onDataEntered: mFollowing [g, l, g, l]
记录 mFollowing ArrayList 的大小: onDataEntered: mFollowing size 2 onDataEntered: mFollowing size 4
我的数据库图片: enter image description here
我期望发生的事情是将用户 ID 添加到 mFollowing 中,当我运行 mFollowing 的日志时,我只看到数组中有 2 个用户 ID,计数为 2。
【问题讨论】:
-
onDataEntered中的每个dataSnapshot都是一个用户,所以只需使用dataSnapshot.getKey添加到mFollowing。 -
但这不是你的代码正在做的事情——你正在使用“儿童”循环下降一级——不要这样。
-
你会想,但是当我按照你的方式去做并在另一种方法中使用
mFollowing.size()for( int i = 0; i < mFollowing.size(); i++){ //insert code}我得到一个看起来像 1、2、2 而不是 2 的大小。做同样的事情使用用户 ID。
标签: java android firebase geofire