我的代码我希望从我的firebase数据库中获取帖子,但onKeyEntered从未调用,检查我的代码中是否有问题我写了一些toast来检查,但我发现onQueryReady被调用为预期但问题出在onKeyEntered上,我甚至在特定帖子的位置上设置了静态位置,实际上有很多帖子都有这个位置,但它仍然是相同的,并且onKeyEntered从未调用过。请帮忙。
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
posts = new ArrayList<>();
//initialization work of the recycler view
View rootView = inflater.inflate(R.layout.posts_fragment, container, false);
//initialization work of the firebase database
firebaseInitializationWork();
return recyclerViewInitializationWork(rootView);
}
@Override
public void onStart() {
super.onStart();
geoQueryToSearchPosts.addGeoQueryEventListener(geoQueryEventListener);
}
@Override
public void onStop() {
super.onStop();
geoQueryToSearchPosts.removeAllListeners();
}
private void firebaseInitializationWork() {
if (posts == null)
posts = new ArrayList<>();
//setting up the reference and the geoquery objects
postsReference = FirebaseDatabase.getInstance().getReference().child("posts");
geofireToSearchPosts = new GeoFire(postsReference);
//set the query on the current location and around the user with 1 kilo meter.
updateLocation();
geoQueryToSearchPosts = geofireToSearchPosts.queryAtLocation(
/*getLastKnownLocation()*/new GeoLocation(29.9061584,31.2710861), 1);
//creating the listener and adding it to the geoQueryToSearchPosts.
attachTheGeoQueryListener();
}
private GeoLocation getLastKnownLocation() {
GeoLocation geoLocation = null;
/*if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}*/
Location location = locationManager.getLastKnownLocation("gps");
geoLocation = new GeoLocation(location.getLatitude() , location.getLongitude());
return geoLocation;
}
//function to initialize the geofire query listener
//and attach it to the geofire query object (geoQueryToSearchPosts)
private void attachTheGeoQueryListener() {
if (geoQueryEventListener == null) {
geoQueryEventListener = new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
Toast.makeText(getActivity() , "onKeyEntered" , Toast.LENGTH_LONG).show();
//retrieving the post by listening on the post node.
postsReference.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//adding the post to the array.
posts.add(0, (PostDataClass) dataSnapshot.getValue());
// notifying the adapter that there is
//an element inserted.
mAdapter.notifyItemInserted(0);
//scroll to the beginning of the list
mRecyclerView.smoothScrollToPosition(0);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onKeyExited(String key) {
postsReference.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//deleting the post from the posts array and notify the adapter that
//the post the postion has been deleted.
PostDataClass post = (PostDataClass) dataSnapshot.getValue();
int postPosition = posts.indexOf(post);
posts.remove(post);
mAdapter.notifyItemRemoved(postPosition);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
Toast.makeText(getActivity() , "onGeoQueryReady" , Toast.LENGTH_LONG).show();
}
@Override
public void onGeoQueryError(DatabaseError error) {
Toast.makeText(getActivity() , "onGeoQueryError" , Toast.LENGTH_LONG).show();
}
};
//geoQueryToSearchPosts.addGeoQueryEventListener(geoQueryEventListener);
}
}
}
答案 0 :(得分:1)
我找到了问题的解决方案,问题是我正在收听帖子节点而不是geofire节点,因此它没有看到任何位置进入我关注的区域。
答案 1 :(得分:0)
我有一个非常相似的问题...试图将标记添加到我的Google地图onKeyEntered ...我在onKeyEntered方法中添加了runOnUiThread。
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(final String key, GeoLocation location) {
Log.d(TAG, "Key Entered");
TrackOrderFragment.this.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mDriverMarker = googleMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(carIcon))
.position(selectedDriver)
.title(key));
mDriverMarker.setTag(key);
}
});
}
}
真的希望它对您有用...谢谢