根据条件在recyclerview上显示项目

时间:2019-04-05 16:02:16

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

我正在向我的移动应用程序添加聊天功能。它具有两个选项卡:“聊天”和“用户”。但是,在“用户”选项卡中,我只想显示数据库中作为管理员的“用户”。以下是我的UserNormal和ReadUsers代码。请帮忙。谢谢!

ScreenShot

数据库结构:

Firebase

UsersFragment

public class UsersFragmentTwo extends Fragment {

private RecyclerView recyclerView;

private UserAdapter userAdapter;
private List<UserNormal> mUsers;

EditText search_users;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_users, container, false);

    recyclerView = view.findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    mUsers = new ArrayList<>();

    readUsers();
   }

ReadUsersCode:

    private void readUsers() {

    final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (search_users.getText().toString().equals("")) {
                mUsers.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    UserNormal user = snapshot.getValue(UserNormal.class);

                    if (!user.getId().equals(firebaseUser.getUid())) {
                        mUsers.add(user);
                    }

                }

                userAdapter = new UserAdapter(getContext(), mUsers, false);
                recyclerView.setAdapter(userAdapter);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

UserNormal

public class UserNormal {

private String id;
private String username;
private String imageURL;
private String status;
private String search;
private String isAdmin;
private String email;
private String firstName;
private String lastName;



public UserNormal(String id, String username, String imageURL, String status, String search, String email) {
    this.id = id;
    this.email = email;
    this.username = username;
    this.imageURL = imageURL;
    this.status = status;
    this.search = search;
    this.firstName = firstName;
    this.lastName = lastName;
    isAdmin =  "false";
}

public UserNormal() {

}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getIsAdmin() {
    return isAdmin;
}

public void setIsAdmin(String isAdmin) {
    this.isAdmin = isAdmin;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getImageURL() {
    return imageURL;
}

public void setImageURL(String imageURL) {
    this.imageURL = imageURL;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getSearch() {
    return search;
}

public void setSearch(String search) {
    this.search = search;
}
}

1 个答案:

答案 0 :(得分:0)

尝试查询

private void readUsers() {

    final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    Query qry = FirebaseDatabase.getInstance().getReference("Users").orderByChild("isAdmin").equalTo("true");

    qry.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (search_users.getText().toString().equals("")) {
                mUsers.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    UserNormal user = snapshot.getValue(UserNormal.class);

                    if (!user.getId().equals(firebaseUser.getUid())) {
                        mUsers.add(user);
                    }

                }

                userAdapter = new UserAdapter(getContext(), mUsers, false);
                recyclerView.setAdapter(userAdapter);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}