gpt4 book ai didi

android - 根据条件在recyclerview上显示项目

转载 作者:行者123 更新时间:2023-11-29 02:21:11 25 4
gpt4 key购买 nike

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

ScreenShot

数据库结构:

Firebase

用户 fragment

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

读取用户代码:

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

}
});
}

用户正常

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

最佳答案

尝试查询

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

}
});
}

关于android - 根据条件在recyclerview上显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55539406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com