2017-04-16 3 views
0

私のコード私のfirebaseデータベースからの投稿を得ることを期待しているが、onKeyEnteredは決して呼び出されず、コードに問題があるかどうかをチェックするためにトーストを書いたが、しかし、問題はonKeyEnteredにあります。私は特定の投稿の場所に静的な場所を作成しましたが、実際にはこの場所の投稿がたくさんありますが、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

誰かが助けられる可能性を高めるには、コードを[問題を再現するために必要な最小限]に減らしてください(http://stackoverflow.com/help/mcve)(リンクを読んでください。有用)。 –

+0

クロスポスト:https://github.com/firebase/geofire-java/issues/67 –

+0

このコードでお願いします。私はすでにそれを減らしました。 –

答えて

1

私は、私の問題への解決策を見つけた問題は、私はポストノードではないgeofireノードに聞いているので、それが問題の私の面積を入力するための任意の場所を見ていないということでした。

関連する問題