Я добавляю функцию чата в мое мобильное приложение. Он имеет две вкладки: Чаты и Пользователи. Однако на вкладке Пользователи я хочу отображать только пользователей, которые являются администраторами в базе данных. Ниже приведены мой код для UserNormal и ReadUsers. Пожалуйста помоги. Спасибо!
ScreenShot
Структура базы данных:
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;
}
}