问题描述
我正在使用Firebase查询附近的位置,当我执行systemPrint时,结果是正确的。但是,当我试图创建一个新的位置项目,并把它放在一个全局声明的数组列表中,然后循环通过ArrayList打印它。它不打印。我尝试了一个类似的哈希集我声明一个全局哈希集,并试图添加id和地理位置,然后通过它循环,它不打印。这是打印输出,当我做systemPrint时:
03 -05 21:42:16.225 12604-12604 /? I / System.out:aa6b6c55-40da-416e-bbc0-626f10d2db80 51.02878131 -114.13542057
03-05 21:42:16.262 12604-12604 /? I / System.out:1f682c8b-be9a-4310-aa92-216f041f0547 51.02933723 -114.13514678
03-05 21:42:16.262 12604-12604 /? I / System.out:707a5af3-8fa0-4f69-b88f-593a0acd3ee8 51.02933723 -114.13514678
arrayList全球
列表<位置> locationList; (){
......
locationList = new ArrayList<>();
在onCreate
......
}
geoFire =新的GeoFire(新的Firebase(https://xyz.firebaseio.com/位置/));
GeoLocation center = new GeoLocation(location.getLatitude(),location.getLongitude());
final HashSet hs = new HashSet();
GeoQuery geoQuery = geoFire.queryAtLocation(center,5);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener(){
@Override $ b $ public void onKeyEntered(String username,GeoLocation location){
System.out.println(username + location.latitude + location.longitude);
Locations eachLocation = new Locations();
eachLocation.setId(username);
eachLocation.setLatitude(String.valueOf(location。 latitude));
eachLocation.setLongitude(String.valueOf(location.longitude));
locationList.add(eachLocation);
/ /
//
$ b @Override $ b $ public void onKeyExited(String username){
usersNearby.remove(username);
//附加代码,例如从地图中移除一个图钉
//移除此用户的任何Firebase侦听器
}
@Override
public void onKeyMoved(String s,GeoLocation geoLocation){
$ b @Override $ b $ public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(FirebaseError firebaseError){
});
(Locations x:locationList){
Log.i(eachLocation,x.getId()+ x.getLatitude()+ x.getLongitude());
}
System.out.println(hs);
这是我的Locations类
public class Locations {
String id;
字符串纬度;
字符串经度;
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getLatitude(){
return latitude;
}
public void setLatitude(String latitude){
this.latitude = latitude;
}
public String getLongitude(){
return longitude;
}
public void setLongitude(String longitude){
this.longitude = longitude;
code
$ b $ p
$ b *****更新 - 我也注意到另一种模式,我不能在另一个Firebase查询内执行功能****
我声明了一个全局的userCountry变量,userCountry ,然后我运行一个查询
获取用户所保存的国家。在功能里面,它记录的很好。但是,当我尝试将国家值设置为userCountry,并将其记录在查询以外,我的应用程序崩溃和NullPointerException异常。我需要全局设置userCountry,所以我可以将它作为参数提供给另一个查询。
字符串userCountry;
ref = new Firebase(https://xyzChat.firebaseio.com/users/+ intent.getStringExtra(userId)+/ country);
查询queryRef = ref.orderByChild(country);
queryRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.i(userCountry,String.valueOf (dataSnapshot.getValue()));
userCountry = String.valueOf(dataSnapshot.getValue());
}
Log.i(secondUserCountry,userCountry);
解决方案事件被异步触发在GeoFire中,所以当你打印出 locationList
时,它仍然是空的,你需要等到 onGeoQueryReady
被调用,直到所有的元素都被添加了,如果你把日志代码移动到那里,它应该打印所有的位置,每次更新查询时都要调用 onGeoQueryReady
。
I am querying nearby locations using Firebase and the results are correct when I do a systemPrint. But when I tried to create a new Location item and put it in a globally declared arrayList then loop through the ArrayList to print it. It doesn't print. I tried something similar with a hashset where I declare a global hashset and tried adding the id and geolocation to it, then loop through it, it didn't print.
This is the printout when I do a systemPrint:
03-05 21:42:16.225 12604-12604/? I/System.out: aa6b6c55-40da-416e-bbc0-626f10d2db80 51.02878131 -114.13542057
03-05 21:42:16.262 12604-12604/? I/System.out: 1f682c8b-be9a-4310-aa92-216f041f0547 51.02933723 -114.13514678
03-05 21:42:16.262 12604-12604/? I/System.out: 707a5af3-8fa0-4f69-b88f-593a0acd3ee8 51.02933723 -114.13514678
I set this arrayList globally
List<Locations> locationList;
in onCreate(){
......
locationList = new ArrayList<>();
......
}
geoFire = new GeoFire(new Firebase("https://xyz.firebaseio.com/locations/"));
GeoLocation center = new GeoLocation(location.getLatitude(), location.getLongitude());
final HashSet hs = new HashSet();
GeoQuery geoQuery = geoFire.queryAtLocation(center, 5);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String username, GeoLocation location) {
System.out.println(username + location.latitude + location.longitude);
Locations eachLocation = new Locations();
eachLocation.setId(username);
eachLocation.setLatitude(String.valueOf(location.latitude));
eachLocation.setLongitude(String.valueOf(location.longitude));
locationList.add(eachLocation);
//
// }
}
@Override
public void onKeyExited(String username) {
usersNearby.remove(username);
// additional code, like removing a pin from the map
// and removing any Firebase listener for this user
}
@Override
public void onKeyMoved(String s, GeoLocation geoLocation) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(FirebaseError firebaseError) {
}
});
for (Locations x: locationList){
Log.i("eachLocation", x.getId() + x.getLatitude() + x.getLongitude());
}
System.out.println(hs);
This is my Locations class
public class Locations {
String id;
String latitude;
String longitude;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
}
*****UPDATE - I am also noticing another pattern where I can not perform function inside another Firebase query****
I declared a global userCountry variable, userCountry, and then I run a query toobtain the user's country that is saved. Inside the function, it logs fine. But when i try to set the country value to userCountry, and log it outside of the query, my app crashes and errors out on a NullPointerException. I need to set userCountry globally so I can feed it into another query as a parameter.
String userCountry;
ref = new Firebase("https://xyzChat.firebaseio.com/users/" + intent.getStringExtra("userId") + "/country");
Query queryRef = ref.orderByChild("country");
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("userCountry", String.valueOf(dataSnapshot.getValue()));
userCountry = String.valueOf(dataSnapshot.getValue());
}
Log.i("secondUserCountry", userCountry);
解决方案 Events are fired asynchronously in GeoFire. So at the time you print out the locationList
it will still be empty. You will need to wait until onGeoQueryReady
is called until all elements have been added. If you move the logging code into there, it should print all locations. Note that onGeoQueryReady
is called every time you update the query.
这篇关于在GeoFire和Firebase查询中执行函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!