所以,我使用这个名为GeoFire的库(src:https://github.com/firebase/geofire-java)来获取用户在一定距离内发布的帖子。在我的代码中,我做了一个这样的方法:
private HashMap<String, GeoLocation> getPostAroundUser(DatabaseReference rootRef) {
final HashMap<String, GeoLocation> postHashMapLocation = new HashMap<String, GeoLocation>();
DatabaseReference refPostGeo = rootRef.child("geo").child("posts");
final GeoFire geoFire = new GeoFire(refPostGeo);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(mLatitude, mLongitude), 3);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
Log.i(TAG, "Post with key: " + key + " was added to hash with location (lat, long): (" + location.latitude + ", " + location.longitude + ")");
postHashMapLocation.put(key, location);
Log.i(TAG, "number of entry after added " + postHashMapLocation.size());
}
@Override
public void onKeyExited(String key) {
Log.i(TAG, "Post with key: " + key + " was removed");
postHashMapLocation.remove(key);
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
Log.i(TAG, "number of entry onGeoQueryReady " + postHashMapLocation.size());
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
Log.i(TAG, "total entry in function: " + postHashMapLocation.size()); //postHashMapLocation.size() returns 0
return postHashMapLocation;
}
但是这个方法返回一个空的HashMap
,所以我添加了一些日志来查看发生了什么,这里是日志:
02-21 18:09:32.609 15115-15115/com.example.company.app I/ExampleActivity: total entry in function: 0
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7DPzEZHSdGHqfbmna was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 1
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7DULwW01S9-haorm9 was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 2
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7Dt6uvs50qddJHxiA was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 3
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7EOF41MijwSfrxpX1 was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 4
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB3mJvB0K6nSCtNnoL was added to hash with location (lat, long): (-6.1333898, 106.6861029)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 5
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB3r13H7y3jiovlLdi was added to hash with location (lat, long): (-6.1333898, 106.6861029)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 6
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB58ArIOFKHrfEnYpr was added to hash with location (lat, long): (-6.1335054, 106.6910521)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 7
02-21 18:09:32.869 15115-15115/com.example.company.app I/ExampleActivity: number of entry onGeoQueryReady 7
在HashMap
内的日志之前返回geoQuery
之前,我发现了日志。这就是该方法返回空HashMap
所以,我的问题是,如何通过此方法返回HashMap
内的onKeyEntered
?正如您从日志中看到的那样HashMap
已在onKeyPressed
内更新,但我无法将其检索到geoQuery
的外部。
注意:我在Error performing functions inside a GeoFire and Firebase query找到了类似的问题,但没有答案。
答案 0 :(得分:0)
Geofire回调(例如Traceback (most recent call last):
File "t2g2g.py", line 21, in <module>
data= a.get_site_t2g_eg_resolved_dos(structure)
File "/usr/local/lib/python2.7/dist-packages/pymatgen/electronic_structure/dos.py", line 351, in get_site_t2g_eg_resolved_dos
for s, atom_dos in self.pdos.items():
AttributeError: 'list' object has no attribute 'items'
)是异步调用的......很可能是在您从onKeyEntered
方法返回后。您需要相应地构建应用程序。
答案 1 :(得分:0)
您的hashmap为空的原因是因为您已将其设置为最终变量,因此将其初始化为空集。最后意味着它是不可改变的,因此不能改变。所以当你初始化它时,它是空的,因此将保持空白