如何从所有用户的FirebaseRecyclerAdapter隐藏当前用户?

时间:2019-04-24 19:27:30

标签: java android firebase firebase-realtime-database android-recyclerview

当前,它会提取所有用户的详细信息,并填充其图像,名称和简历。我希望它从用户列表中隐藏当前用户,但是我不知道如何实现该代码。

我尝试使用if/while语句将dataSnapshot与当前用户进行比较,但是我不知道如何根据这种情况来填充用户。

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);
            }
        }
    }
}

我希望当前用户将从RecyclerView中的所有用户列表中隐藏。预先感谢!

1 个答案:

答案 0 :(得分:0)

您可以考虑如下修改databaseReference

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);

现在,在设置FirebaseRecyclerAdapter的选项时,您可以考虑如下设置查询。

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

我还没有测试过。但是,请让我知道是否可行。