简体   繁体   English

在android studio中将用户列表按字母顺序排序

[英]sorting user list to alphabetical order in android studio

I am making a chat application and want to arrange user in users list from firebaseStore in alphabetical order.我正在制作一个聊天应用程序,并希望按字母顺序在 firebaseStore 的用户列表中排列用户。 if new users register with the application, these users will be added to the list automatically and arrange in alphabetical order ( like A to B to C, ..).如果新用户在应用程序中注册,这些用户将自动添加到列表中并按字母顺序排列(如 A 到 B 到 C,..)。 I try to search the answer but I don't find the solution to my issue.我尝试搜索答案,但找不到解决问题的方法。 Can anyone help me with how to solve it?谁能帮我解决它?

Here is my java code in Userfragment.java:这是我在 Userfragment.java 中的 java 代码:

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

    mUsers = new ArrayList<>();



    imageProfile = view.findViewById(R.id.profile_image);
    username = view.findViewById(R.id.username);

    storageReference = FirebaseStorage.getInstance().getReference("uploads");

    fuser = FirebaseAuth.getInstance().getCurrentUser();
   
    Query query = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid()).orderByChild("username");


    Collections.sort(mUsers, new Comparator<User>() {
        @Override
        public int compare(User lhs, User rhs) {
            return lhs.getUsername().compareTo(rhs.getUsername());
        }
    });

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (isAdded()) {

                User user = dataSnapshot.getValue(User.class);
                username.setText(user.getUsername());
                if (user.getImageUrl().equals("default")) {
                    imageProfile.setImageResource(R.mipmap.ic_launcher);
                } else {
                    Glide.with(getContext()).load(user.getImageUrl()).into(imageProfile);
                }
            }

        }

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

        }
    });

导出 Json 文件

below is some users in realtime database which I want to arrange:以下是我要安排的实时数据库中的一些用户:

Users 7HMH8nj6AshgYtvD2IMT9a7y2273用户 7HMH8nj6AshgYtvD2IMT9a7y2273
id "7HMH8nj6AshgYtvD2IMT9a7y2273" imageUrl "default" search "tony" status "online" username "Tony" AY3gY1eQE1fWJiNUsxxRqpXHBFf2 id "7HMH8nj6AshgYtvD2IMT9a7y2273" imageUrl "default" search "tony" status "online" 用户名 "Tony" AY3gY1eQE1fWJiNUsxxRqpXHBFf2
id "AY3gY1eQE1fWJiNUsxxRqpXHBFf2" imageUrl "default" search "hola" status "offline" username "hola" O6gRe8muq4TGDjQsYkn9Cdpufdy1 id "AY3gY1eQE1fWJiNUsxxRqpXHBFf2" imageUrl "default" search "hola" status "offline" 用户名 "hola" O6gRe8muq4TGDjQsYkn9Cdpufdy1
id "O6gRe8muq4TGDjQsYkn9Cdpufdy1" imageUrl "default" search "chon" status "offline" username "chon" SGQwWbIZ2vhkPcKqjMuejusKRY13 id "O6gRe8muq4TGDjQsYkn9Cdpufdy1" imageUrl "default" search "chon" status "offline" username "chon" SGQwWbIZ2vhkPcKqjMuejusKRY13
id "SGQwWbIZ2vhkPcKqjMuejusKRY13" imageUrl "default" search "miko" status "offline" username "miko" id "SGQwWbIZ2vhkPcKqjMuejusKRY13" imageUrl "default" search "miko" status "offline" username "miko"

Try this尝试这个

FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("username");

Learn more here 在这里了解更多

You're getting the following error:您收到以下错误:

java.lang.ClassCastException: com.google.firebase.database.Query cannot be cast to com.google.firebase.database.DatabaseReference java.lang.ClassCastException:com.google.firebase.database.Query 无法转换为 com.google.firebase.database.DatabaseReference

Because of the following line of code:由于以下代码行:

reference = (DatabaseReference) FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid()).orderByChild("username");

And this is because you are trying to cast an object of type Query to an object of type DatabaseReference , which is actually not possible in Java.这是因为您试图将Query类型的对象强制转换为DatabaseReference类型的对象,这在 Java 中实际上是不可能的。 The inheritance relationship is as follows, DatabaseReference extends Query and not the opposite.继承关系如下,DatabaseReference 继承 Query 而不是相反。

To solve the problem, you should remove the cast to DatabaseReference and set the type of the object to be Query:要解决此问题,您应该删除对 DatabaseReference 的强制转换并将对象的类型设置为 Query:

Query query =  FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid()).orderByChild("username");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM