Want to order chats in ArrayList by timestamp using Comparator, but it's not working and I don't know why

huangapple go评论83阅读模式
英文:

Want to order chats in ArrayList by timestamp using Comparator, but it's not working and I don't know why

问题

ChatFragment

public class ChatsFragment extends Fragment {

    // ... (Other code remains unchanged)

    private void chatList() {
        mUsers = new ArrayList<>(mChatList.size());
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUsers.clear();
                Collections.sort(mChatList, (o1, o2) -> Long.compare(o2.getTimestamp(), o1.getTimestamp())); // Reversed order for newest first
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    User user = snapshot.getValue(User.class);
                    for (int i = 0; i < mChatList.size(); i++) {
                        if (mFirebaseUser != null && user != null) {
                            if (!user.getId().equals(mFirebaseUser.getUid()) && user.getId().equals(mChatList.get(i).receiver)) {
                                ensureSize((ArrayList<?>) mUsers, mChatList.size());
                                mUsers.set(i, user);
                            }
                        }
                    }
                }

                mUserAdapterChat = new UserAdapterChat(getContext(), mUsers, true);
                mUserAdapterChat.notifyDataSetChanged();
                mRecyclerView.setAdapter(mUserAdapterChat);
            }

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

            }
        });
    }

    // ... (Other code remains unchanged)
}

Please note that I've made a slight modification to the Collections.sort() line to reverse the order and display the newest messages first.

英文:

I have implemented a chat function in my app and it's working fine, only problem is that now I want the chats to be ordered by timestamp, so that the newest ones come first and the oldest come last. I have tried doing so using Comparator, but for some reason it's not working and I am not sure how to fix it.

Below you have my MessageActivity where I save the chats to the Firebase, and my ChatFragment that contains the ArrayList for the chats. The chats are appearing there, but I can't get them by any means to rearrange themselves and get in order by Timestamp.

Someone mind telling me how to fix this?

Want to order chats in ArrayList by timestamp using Comparator, but it's not working and I don't know why

ChatFragment

public class ChatsFragment extends Fragment {
private RecyclerView mRecyclerView;
private UserAdapterChat mUserAdapterChat;
private List&lt;User&gt; mUsers;
private List&lt;Chatlist&gt; mChatList;
private FirebaseUser mFirebaseUser;
private TextView mNoMessages;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_chats, container, false);
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mNoMessages = v.findViewById(R.id.no_messages);
mRecyclerView = v.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(linearLayoutManager);
mChatList = new ArrayList&lt;&gt;();
mUserAdapterChat = new UserAdapterChat(getContext(), mUsers, false);
mRecyclerView.setAdapter(mUserAdapterChat);
mUsers = new ArrayList&lt;&gt;();
EditText search_users = v.findViewById(R.id.search_bar);
search_users.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchUsers(s.toString().toLowerCase());
}
@Override
public void afterTextChanged(Editable s) {
}
});
DatabaseReference reference = FirebaseDatabase.getInstance().getReference(&quot;Chatlist&quot;).child(mFirebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d(&quot;CHAT&quot;, dataSnapshot.toString());
mChatList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Chatlist chatlist = snapshot.getValue(Chatlist.class);
mChatList.add(chatlist);
}
if (mChatList.size() == 0) {
mNoMessages.setVisibility(View.VISIBLE);
} else {
mNoMessages.setVisibility(View.GONE);
chatList();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Task&lt;InstanceIdResult&gt; task = FirebaseInstanceId.getInstance().getInstanceId();
task.addOnCompleteListener(task1 -&gt; {
if (task1.isSuccessful()) {
String token = task1.getResult().getToken();
updateToken(token);
} else {
Exception exception = task1.getException();
Log.d(&quot;TOKEN&quot;, exception.getMessage());
}
});
return v;
}
private void updateToken(String token) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference(&quot;Tokens&quot;);
Token token1 = new Token(token);
reference.child(mFirebaseUser.getUid()).setValue(token1);
}
private void searchUsers(String s) {
Query query = FirebaseDatabase.getInstance().getReference(&quot;Users&quot;).orderByChild(&quot;username&quot;).startAt(s).endAt(s + &quot;\uf8ff&quot;);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
if (s.length() == 0) {
mUsers.clear();
} else {
if (user != null) {
if (!user.getId().equals(mFirebaseUser.getUid())) {
mUsers.add(user);
mNoMessages.setVisibility(View.GONE);
}
}
}
}
mUserAdapterChat = new UserAdapterChat(getContext(), mUsers, false);
mUserAdapterChat.notifyDataSetChanged();
mRecyclerView.setAdapter(mUserAdapterChat);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void chatList() {
mUsers = new ArrayList&lt;&gt;(mChatList.size());
DatabaseReference reference = FirebaseDatabase.getInstance().getReference(&quot;Users&quot;);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
Collections.sort(mChatList, ChatsFragment.this::compare);
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
for (int i = 0; i &lt; mChatList.size(); i++) {
if (mFirebaseUser != null &amp;&amp; user != null) {
if (!user.getId().equals(mFirebaseUser.getUid()) &amp;&amp; user.getId().equals(mChatList.get(i).receiver)) {
ensureSize((ArrayList&lt;?&gt;) mUsers, mChatList.size());
mUsers.set(i, user);
}
}
}
}
mUserAdapterChat = new UserAdapterChat(getContext(), mUsers, true);
mUserAdapterChat.notifyDataSetChanged();
mRecyclerView.setAdapter(mUserAdapterChat);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public static void ensureSize(ArrayList&lt;?&gt; list, int size) {
list.ensureCapacity(size);
while (list.size() &lt; size) {
list.add(null);
}
}
public int compare(Chatlist o1, Chatlist o2) {
return o1.getTimestamp() &lt; o2.getTimestamp() ? 1 : (o1.getTimestamp() == o2.getTimestamp() ? 0 : -1);
}
}

答案1

得分: 2

你正在使用Long.compare()按升序对集合进行排序。
要显示最新的聊天,您需要更改比较方法,如下所示:

public int compare(Chatlist o1, Chatlist o2) {
    return o1.getTimestamp() < o2.getTimestamp() ? 1 : 
           (o1.getTimestamp() == o2.getTimestamp() ? 0 : -1);
}
英文:

You are sorting collection in ascending order by using Long.compare().
For showing latest chat you need to change the compare method as

public int compare(Chatlist o1, Chatlist o2) {
return o1.getTimestamp() &lt; o2.getTimestamp() ? 1 : 
(o1.getTimestamp == o2.getTimestamp() ? 0 : -1);
}

答案2

得分: 0

你可以简单地使用Java 8的特性来进行元素比较,如下所示:

List<ChatList> unique = list.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparing(ChatList::getTimestamp))), ArrayList::new));
unique.forEach((k) -> System.out.println(k.getTimestamp()));
英文:

You can simply use Java8 feature to compare the element as below:

List&lt;ChatList&gt; unique = list.stream().collect(collectingAndThen(toCollection(() -&gt; new TreeSet&lt;&gt;(comparing(ChatList::getTimestamp))),ArrayList::new));
unique.forEach((k) -&gt; System.out.println(k.getTimestamp()));

huangapple
  • 本文由 发表于 2020年7月25日 23:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63090111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定