使用Firebase在我的聊天应用程序中扰乱聊天信息



我最近添加了一个发送图像的功能,以及发送文本消息的选项。在此之前,一切都很好——消息按预期弹出,按降序查询。但在我添加了这个图像发送功能后,每当我在聊天中向上或向下滚动时,消息都会变得一团糟,我不知道为什么。

当我只发送文本时:

滚动之前https://i.stack.imgur.com/2NMVHl.jpg

滚动后https://i.stack.imgur.com/2Nxpc.jpg(相同(

当我发送带有文本的图像时

滚动之前https://i.stack.imgur.com/rS6Mx.jpg

滚动后https://i.stack.imgur.com/OP5DK.jpg

在我的代码中有两个地方可以上传消息(图像或文本(。

  1. 发送图片信息:

简报-在这里,我将图像上传到存储器,然后检索其URL。然后,我将URL以字符串的形式存储到firestore(我后来用它来使用Picasso下载图像(以及其他消息详细信息。

final Long currentTime = System.currentTimeMillis();
final String time = currentTime + "";
final StorageReference fileref = storageReference.child("Image Messages")
.child(uid + time);
StorageTask uploadTask = fileref.putFile(uri);
uploadTask.continueWithTask(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if(!task.isSuccessful()){
throw task.getException();
}
return fileref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
Uri downloadUrl = task.getResult();
String myUrl = downloadUrl.toString();
Map<String, Object> chat = new HashMap<>();
chat.put("message", myUrl);
chat.put("time", currentTime);
chat.put("sender", mUser.getUid());
chat.put("groupId",Gid);
chat.put("type","image");
chat.put("username",preferences.getData("username"));
chat.put("name",preferences.getData("usernameAdded"));
firestore.collection("aGroups").document(Gid).collection("Chat")
.add(chat).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
showChatMessages();
dialog.dismiss();
Toast.makeText(getApplicationContext(),"Message Sent", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
});
}
}
});
  1. 用于发送短信

简报-在这里我上传消息,它的详细信息

Map<String, Object> chat = new HashMap<>();
chat.put("message", message.getText().toString());
chat.put("time", System.currentTimeMillis());
chat.put("sender", mUser.getUid());
chat.put("groupId",Gid);
chat.put("type","text");
chat.put("username",preferences.getData("username"));
chat.put("name",preferences.getData("usernameAdded"));
firestore.collection("aGroups").document(Gid).collection("Chat")
.add(chat).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(ChatInterface.this, "Message Sent", Toast.LENGTH_SHORT).show();
showChatMessages();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
});

这是我添加回调的地方

private void showChatMessages() {
firestore.collection("aGroups").document(Gid).collection("Chat")
.orderBy("time", Query.Direction.DESCENDING)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if(error != null){
Log.d("Check", "listen failed: " + error.getMessage());
}
else{
Log.d("Check", "Snapshot worked");
List<ChatModel> list = new ArrayList<>();
list.clear();
for(QueryDocumentSnapshot query : value){
list.add(new ChatModel(
query.getString("groupId")
, query.getId()
, query.getString("message")
, query.getString("sender")
, query.getLong("time")
, query.getString("name")
, query.getString("username")
, query.getString("type")
));
}
recycler_interface.setAdapter(new RealChatRecyclerInterface(mUser.getUid(),list));
}}
});
}

我将在这个粘贴框链接中添加我的整个RealChatRecyclerInterface

RealChatRecyclerInterface中使用holder.setIsRecyclable(false);

最新更新