gpt4 book ai didi

android - 如何使用 firebase 按时间顺序显示聊天用户列表?

转载 作者:太空狗 更新时间:2023-10-29 14:36:07 25 4
gpt4 key购买 nike

我需要根据聊天用户使用 firebase 聊天发送给用户的最新消息来安排聊天用户。我试过使用基于时间戳的查询顺序,但它对我不起作用。请在下面找到我的代码和这里的数据库结构

enter image description here

   mDatabaseReference = FirebaseDatabase.getInstance().getReference();
final Query databaseReference = FirebaseDatabase.getInstance().getReference().child(FirebaseConstants.USERS).child(mFirebaseUser).child(FirebaseConstants.COLLEGEMATES).orderByChild("timestamp");
final DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference().child(FirebaseConstants.USERS);
FirebaseRecyclerOptions<Student> options = new FirebaseRecyclerOptions.Builder<Student>().setQuery(databaseReference, Student.class).setLifecycleOwner(getActivity()).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Student, FirebaseListViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final FirebaseListViewHolder holder, final int position, @NonNull final Student model) {
try {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
}

最佳答案

我通过在使用此代码段添加的名为“createdAt”的每条消息中添加 child 来解决此问题:

            String patternForDateExtended = "yyyyMMddHHmmss";
DateFormat dff = new SimpleDateFormat(patternForDateExtended);
Date today = Calendar.getInstance().getTime();
String timeString = df.format(today);
String rightNowEverything = dff.format(today)+rightNow.get(Calendar.MILLISECOND);

例如,输出将是一个新月形数字,如 20190404132156825

然后用这个命令消息:

     myRef.orderByChild("createdAt").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

// adding to the recycleview everything.


}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

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

}
});

希望对你有所帮助,继续编码:)

关于android - 如何使用 firebase 按时间顺序显示聊天用户列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55514036/

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