0

I am using Firebase GeoFire to query results from Firebase I am using custom Firebase Recycler Adapter. I am not sure where I am going wrong I have also checked the similar questions here but non work to resolve my issue can anyone help me figure out the problem.

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        if (savedInstanceState != null) {
            // Restore saved layout manager type.
            mRecyclerViewPosition = (int) savedInstanceState
                    .getSerializable(KEY_LAYOUT_POSITION);
            mRecyclerView.scrollToPosition(mRecyclerViewPosition);
            // TODO: RecyclerView only restores position properly for some tabs.
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        buildGoogleApiClient();
        buildLocationSettingsRequest();
        checkLocationSettings();
        createLocationRequest();
        mFirebaseRef = FirebaseDatabase.getInstance().getReference(POSTS_STRING);
        mFirebaseRef.keepSynced(true);
        this.geoFire = new GeoFire(FirebaseDatabase.getInstance().getReference(GEO_POINTS));
        mAdapter = new FirebasePostsQueryAdapter(mFirebaseRef.equalTo(GEO_POINTS), getActivity());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.geo_fragment_layout, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.geo_recycler_view);

        return rootView;
    }

    private void bindRecyclerView() {
        mRecyclerView.setAdapter(mAdapter);
    }

    private void startGeoQuery() {
        query = geoFire.queryAtLocation(center, 2);
        Log.d(TAG, "center: " + center.toString() + ", radius: " + 2);
        query.addGeoQueryEventListener(new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {
                Log.d(TAG, "Key " + key + " entered the search area at [" + location.latitude + "," + location.longitude + "]");
                final DatabaseReference tempRef = mFirebaseRef.child(key);
                tempRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        // I  add the deal only if it doesn't exist already in the adapter
                        String key = snapshot.getKey();
                        if (!mAdapter.exists(key)) {
                            Log.d(TAG, "item added " + key);
                            mAdapter.addSingle(snapshot);
                            mAdapter.notifyDataSetChanged();
                            mListContainer.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            //...otherwise I will update the record
                            Log.d(TAG, "item updated: " + key);
                            mAdapter.update(snapshot, key);
                            mAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @Override
            public void onKeyExited(String key) {
                Log.d(TAG, "deal " + key + " is no longer in the search area");
                mAdapter.remove(key);
                isListEmpty();

            }

            @Override
            public void onKeyMoved(String key, GeoLocation location) {
                Log.d(TAG, String.format("Key " + key + " moved within the search area to [%f,%f]", location.latitude, location.longitude));
                final DatabaseReference tempRef = mFirebaseRef.child(key);
                tempRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        // I  add the deal only if it doesn't exist already in the adapter
                        String key = snapshot.getKey();
                        if (!mAdapter.exists(key)) {
                            Log.d(TAG, "item added " + key);
                            mAdapter.addSingle(snapshot);
                            mAdapter.notifyDataSetChanged();
                            mListContainer.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            //...otherwise I will update the record
                            Log.d(TAG, "item updated: " + key);
                            mAdapter.update(snapshot, key);
                            mAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @Override
            public void onGeoQueryReady() {
                Log.d(TAG, "All initial data has been loaded and events have been fired!");
                isListEmpty();
                mActiveGeoQuery = true;
            }

            @Override
            public void onGeoQueryError(DatabaseError error) {
                Log.e(TAG, "There was an error with this query: " + error);
                mListContainer.setViewState(MultiStateView.VIEW_STATE_ERROR);
                mActiveGeoQuery = false;
            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }
Contextioner
  • 75
  • 11

0 Answers0