Search code examples
androidfirebasefirebase-realtime-databasegeofire

i want to use GeoFire to Populate Firebase Recycler View in android


I am building a dating app and i want to find people near the location of the user so im using geofire but i dont know how to use it with recycler adapter this is my code

    FirebaseRecyclerOptions<Users> options =
                new FirebaseRecyclerOptions.Builder<Users>()
                        .setQuery(mUsersDatabaseReference, Users.class)
                        .build();
            FirebaseRecyclerAdapter<Users, UserViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Users,UserViewHolder>(options
            ) {
                @NonNull
                @Override
                public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.recycle_list_single_user, parent, false);
                    return new UserViewHolder(view);
                }

                @Override
                protected void onBindViewHolder(@NonNull UserViewHolder userViewHolder, int i, @NonNull Users users) {
                    LinearLayout layout=userViewHolder.mView.findViewById(R.id.all);
                    if (users.getName()==null)layout.setVisibility(View.GONE);
                    else{
                        userViewHolder.setName(users.getName());
                        if(users.getOnline()!=null)userViewHolder.setUserOnline(users.getOnline());
                        userViewHolder.setStatus(users.getStatus());
                        userViewHolder.setImage(users.getImage(),getActivity().getApplicationContext(),users.getSex());
                        final String user_id=getRef(i).getKey();
                        final String userName=users.getName();
                        userViewHolder.mView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent profileIntent=new Intent(getContext(), ProfileActivity.class);
                                profileIntent.putExtra("user_id",user_id);
                                startActivity(profileIntent);
                            }
                        });

                        ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                        if(i%3==1)
                            marginParams.setMargins(0, 150, 0, 0);
                        else if(i>5)
                            marginParams.setMargins(0, -150, 0, 0);
                        else if(i>2)
                            marginParams.setMargins(0, -100, 0, 0);
                        layout.setLayoutParams(marginParams);

                    }
                }
            };
            mUsersList.setAdapter(firebaseRecyclerAdapter);
                firebaseRecyclerAdapter.startListening();
            }
        }

I have searched a lot here and i didnt find any solution


Solution

  • The adapters in FirebaseUI are designed to load data directly from Firebase Realtime Database or Cloud Firestore. They are not integrated with GeoFire/GeoFirestore.

    If you want to display data based on geoqueries, you will have to build your own adapter. When the onKeyEntered fires, you'll add the key (or its associated data) to a List and then call notifyDataSetChanged on an adapter that you initialized with that list.

    If you've ever built a custom adapter before, it's pretty much all the same. If you haven't built one before, I'd recommend following a tutorial to get familiar with it. After that, you can use the code for the FirebaseUI adapters for (copy/paste) inspiration.