因此,我使用這個名爲GeoFire的庫(src:https://github.com/firebase/geofire-java)在一定距離內從用戶處獲取帖子。在我的代碼,我做了這樣的方法:使用GeoQuery檢索數據
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
所以,我的問題是,我如何從onKeyEntered
內部HashMap
這個方法返回?因爲從日誌中可以看到,HashMap
已在onKeyPressed
內更新,但我無法將其檢索到geoQuery
的外部。
注:我在這裏發現了類似的問題Error performing functions inside a GeoFire and Firebase query,但沒有答案。
你能給我一個例子嗎? – YorS
這將取決於您的應用程序以及您想要對數據執行的操作。一個常見的例子可能是更新你的'RecyerlView'適配器,或者從這些回調中更新谷歌地圖。 –