使用firebase在我的聊天应用程序中显示无组织的聊天信息

vktxenjb  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(392)

我最近还添加了一个发送图像的功能以及发送文本消息的选项。在那之前一切都很好——消息按预期的时间递减弹出。但在我添加了这个图像发送功能后,每当我在聊天室上下滚动时,信息都会变得杂乱无章,我不知道为什么。
当我只发送文本时:
滚动前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
在我的代码中,有两个地方可以上传消息(图像或文本)。
发送图像信息:
简报-在这里我上传图像到存储,然后检索其网址。然后我将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();
                            }
                        });
                    }
                }
            });

用于发送短信
简报-这里我上传的信息和它的细节

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 在这个粘贴箱链接中。

g2ieeal7

g2ieeal71#

使用 holder.setIsRecyclable(false); 在你真实的聊天室里

相关问题