1

Currently, it pulls in all users details and populates their image, name, and bio. I want it to hide the current user from the list of users and I don't know how to implement the code for it.

I have tried if/while statements to compare dataSnapshot to the current user but I don't know how to populate the users depending on this condition.

public class SearchFragment extends Fragment {
    private RecyclerView usersList;
    private DatabaseReference databaseReference;
    private DatabaseReference userDatabase;
    private DatabaseReference checkName;
    private FirebaseAuth firebaseAuth;
    private String currentUserID;
    private View view;

    public SearchFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view =  inflater.inflate(R.layout.fragment_search, container, false);
        usersList = view.findViewById(R.id.search_users);
        firebaseAuth = FirebaseAuth.getInstance();
        currentUserID = firebaseAuth.getCurrentUser().getUid();
        databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
        userDatabase = FirebaseDatabase.getInstance().getReference();
        checkName = userDatabase.child("Users").child(currentUserID).child("name");
        usersList.setHasFixedSize(true);
        usersList.setLayoutManager(new LinearLayoutManager(getContext()));

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        final FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>()
                .setQuery(databaseReference, Users.class)
                .build();

        FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Users, SearchFragment.UsersViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull SearchFragment.UsersViewHolder holder, int position, @NonNull final Users users) {
                 holder.setName(users.getName());
                 holder.setCity(users.getCity());
                 holder.setImage(users.getImage());

                final String user_ID = getRef(position).getKey();
                final String user_Name = users.getName();
                final String user_Img = users.getImage();

                holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        CharSequence pickOption[] = new CharSequence[]{"View Profile", "Send Message"};

                        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                        builder.setTitle("Select an Option");
                        builder.setItems(pickOption, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (i == 0){
                                    Intent otherProfileIntent = new Intent(getActivity(), OthersProfileActivity.class);
                                    otherProfileIntent.putExtra("user_ID", user_ID);
                                    otherProfileIntent.putExtra("user_Name", user_Name);
                                    otherProfileIntent.putExtra("user_Img", user_Img);
                                    startActivity(otherProfileIntent);
                                }

                                if (i == 1) {
                                    Intent messageIntent = new Intent(getActivity(), MessageActivity.class);
                                    messageIntent.putExtra("user_ID", user_ID);
                                    messageIntent.putExtra("user_Name", user_Name);
                                    messageIntent.putExtra("user_Img", user_Img);
                                    startActivity(messageIntent);
                                }
                            }
                        });

                        builder.show();
                    }
                });
            }

            @NonNull
            @Override
            public SearchFragment.UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.users_layout, parent, false);
                return new SearchFragment.UsersViewHolder(view);
            }
        };

        usersList.setAdapter(adapter);
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    public static class UsersViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public UsersViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }

        public void setName(String name) {
            TextView userNameView = mView.findViewById(R.id.single_userName);
            userNameView.setText(name);
        }

        public void setCity(String name) {
            TextView userNameView = mView.findViewById(R.id.single_userStatus);
            userNameView.setText(name);
        }

        public void setImage(String image) {
            CircleImageView userImage = mView.findViewById(R.id.single_userImg);
            if(!image.equals("default")) {
                Picasso.get().load(image).into(userImage);
            }
        }
    }
}

I am expecting the current user to be hidden from the list of all users in the RecyclerView. Thanks in advance!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
LowKeyLo
  • 57
  • 8
  • To understand better, you want to get all users that exist inside your `Users` node except one, the current authenticated user, right? – Alex Mamo Apr 25 '19 at 08:58
  • That's correct, i just need to display all other users except the current autenticated user. – LowKeyLo Apr 25 '19 at 10:31
  • Thanks for pointing me in the right direction Alex, i'll try and implement it. – LowKeyLo Apr 25 '19 at 10:42

1 Answers1

0

You might consider modifying your databaseReference like the following.

databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
checkName = userDatabase.child("Users").child(currentUserID).child("name");

// This is your updated database reference
databaseReference = FirebaseDatabase.getInstance().getReference()
        .child("Users").child(currentUserID);

Now while setting the options for your FirebaseRecyclerAdapter you might consider setting the query like the following.

final FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>()
    .setQuery(databaseReference.equalTo(null), Users.class)
    .build();

I have not tested this. However, please let me know if that works.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • thanks for the quick reply, the problem is with holder.setName(user.getName()); holder.setCity(user.getCity()); holder.setImage(user.getImage()); being called, it has no way of differenciating between which users to populate, i need a condition before it to say dont populate the recycler view with the current users info, only populate others. – LowKeyLo Apr 24 '19 at 20:12
  • Sorry, I did not get it. I see that you have a `currentUserID` in your code and I suppose this is the current user that you want to exclude from your `RecyclerView`. Hence, I suggest modifying the database query so that it gives you the other users except for the one with `currentUserID`. Please let me know if that makes any sense. – Reaz Murshed Apr 24 '19 at 20:15
  • Getting This Error Incompatible types. Required: com.google.firebase.database.DatabaseReference Found: com.google.firebase.database.Query – LowKeyLo Apr 24 '19 at 20:19
  • Can you please try with the updated code now? – Reaz Murshed Apr 24 '19 at 20:25
  • no luck getting the same error, is there way to hide the current user from the recycler view? – LowKeyLo Apr 24 '19 at 20:28
  • Can you please try now? It should remove the incompatible type error. – Reaz Murshed Apr 24 '19 at 20:34
  • i have got it partiatlly to work with this if(user_ID == checkID){ holder.setIsRecyclable(false); holder.mView.setVisibility(View.GONE); return; } where checkid is current id but it still leaves a gap in the recycler view – LowKeyLo Apr 24 '19 at 20:55
  • So you could not filter out the current user from the database query... – Reaz Murshed Apr 24 '19 at 21:25
  • it can, it removes the visibility of the user item after its created, but since its doing that it leaves a gap of where the item used to be, the placeholder is left behind – LowKeyLo Apr 24 '19 at 21:58