我需要根据他们使用Firebase聊天发送给用户的最新消息来安排聊天用户。我已经尝试过根据时间戳查询使用订单,但是它对我不起作用。请在下面找到我的代码和数据库结构
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) {
}
答案 0 :(得分:0)
我通过在每个带有此代码段的名为“ createdAt”的消息中添加子项来解决了此问题:
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) {
}
});
希望对您有所帮助,继续编码:)