当我从Firebase检索聊天消息时,它会在没有发送时间消息的情况下检索它们
我试过orderbychild(时间戳);但它对我的情况不起作用
Intent intent;
List<ChatModel> data;
private RecyclerView mRecy;
ChatAdapter chatAdapter;
String mId;
Button sendmessage;
private EditText mEditWriteMessage;
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
ChatModel chatModel;
ChatModel chatModel2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mechanic_profile);
sendmessage = findViewById(R.id.sendmessage);
mEditWriteMessage = findViewById(R.id.editWriteMessage);
intent = getIntent();
data = new ArrayList<>();
chatAdapter = new ChatAdapter(getApplicationContext(), data);
mRecy = findViewById(R.id.recy);
mRecy.setHasFixedSize(true);
mRecy.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mRecy.setAdapter(chatAdapter);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("CHHAATS");
DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference("CHHAATS");
databaseReference1.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
chatModel2 = snapshot.getValue(ChatModel.class);
if (chatModel2.getFrom().equals(firebaseUser.getUid()) && chatModel2.getTo().equals(intent.getStringExtra("receiver"))
|| chatModel2.getFrom().equals(intent.getStringExtra("receiver")) && chatModel2.getTo().equals(firebaseUser.getUid())) {
data.add(chatModel2);
}
chatAdapter.notifyDataSetChanged();
mRecy.scrollToPosition(data.size() - 1);
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
chatModel2 = snapshot.getValue(ChatModel.class);
}
if (chatModel2.getFrom().equals(firebaseUser.getUid()) && chatModel2.getTo().equals(intent.getStringExtra("receiver"))
|| chatModel2.getFrom().equals(intent.getStringExtra("receiver")) && chatModel2.getTo().equals(firebaseUser.getUid())) {
data.add(chatModel2);
chatAdapter.notifyDataSetChanged();
mRecy.scrollToPosition(data.size() - 1);
}
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
sendmessage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View v){
String message = mEditWriteMessage.getText().toString();
String receiver = intent.getStringExtra("receiver");
String sender = firebaseUser.getUid();
meth(message, receiver, sender);
}
});
}
void meth (String message, String receiver, String sender){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("CHHAATS");
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("text", message);
hashMap.put("from", sender);
hashMap.put("to", receiver);
reference.child(receiver).push().setValue(hashMap);
}
}
实际结果是消息检索而不查看我需要的是按日期和时间检索消息,但firebase做了另一件事
好的,我认为所有的消息都会按最新的顺序排序,所以我认为问题出在RecyclerView上,我认为在RecyclerView中的解决方案也是如此。
将所有消息提取到您的RecyclerView之后应该放入以下行:
mRecy.smoothScrollToPosition(mRecy.getAdapter().getItemCount());
希望它能帮到你:)