无法使用Android中的Firestore使用addSnapshotListener加载图像

时间:2018-10-24 01:48:24

标签: android google-cloud-firestore

我正在尝试在Android中构建一个实时聊天应用程序,在我的OnCreate中,我能够获得我想要的所有对话,但是当我在OnStart方法中使用OnSnapShotsListener时,对话中的图像无法加载,如果我没有使用OnSnapShotsListener应用程序运行良好,所以如果有人可以帮助我将不胜感激。我也放了我的适配器类

 public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyViewHolder>{
        private List<Comment> commentList;
        private FirebaseFirestore nFirebaseFirestore;
        private DocumentReference nDocumentReference;

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View commentView = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_view, parent, false);
            return new MyViewHolder(commentView);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, int position) {
            nFirebaseFirestore = FirebaseFirestore.getInstance();
            Comment comment = commentList.get(position);
            boolean isPhoto = comment.getImagePost() != null;
            if(isPhoto)
            {
                holder.textViewComment.setVisibility(View.GONE);
                holder.imageViewCommentPic.setVisibility(View.VISIBLE);
                Glide.with(CommentActivity.this).load(comment.getImagePost()).into(holder.imageViewCommentPic);
            }else{
                holder.textViewComment.setVisibility(View.VISIBLE);
                holder.imageViewCommentPic.setVisibility(View.GONE);
            }
            holder.textViewComment.setText(comment.getCommentContent());
            String id = comment.getCustomerKey();
            nDocumentReference = nFirebaseFirestore.document("Customer/"+id);
            nDocumentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    Customer customer = documentSnapshot.toObject(Customer.class);
                    holder.textViewCustomerName.setText(customer.getName());
                    if(!(customer.getProfilePic().equals(""))) {
                        Glide.with(CommentActivity.this).load(customer.getProfilePic()).into(holder.imageViewCustomerPic);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return commentList.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder{
            private TextView textViewCustomerName, textViewComment;
            private ImageView imageViewCustomerPic, imageViewCommentPic, imageViewMenuButton;

            public MyViewHolder(View view)
            {
                super(view);
                textViewCustomerName = (TextView)view.findViewById(R.id.textViewCustomerName);
                textViewComment = (TextView)view.findViewById(R.id.textViewComment);
                imageViewCustomerPic = (ImageView)view.findViewById(R.id.imageViewCustomerPic);
                imageViewCommentPic = (ImageView)view.findViewById(R.id.imageViewCommentPic);
                imageViewMenuButton = (ImageView)view.findViewById(R.id.imageViewMenuButton);
            }
        }

        public CommentAdapter(List<Comment> cList)
        {
            commentList = cList;
        }
    }

如果有人可以帮助我,将不胜感激。下面是我的编码

protected void onStart() {
        super.onStart();
        mCollectionReference.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                listComment.clear();
                for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots)
                {
                    Comment comment = documentSnapshot.toObject(Comment.class);
                    String commentContent = comment.getCommentContent();
                    String customerKey = comment.getCustomerKey();
                    Date time = comment.getPostTime();
                    String commentImage = comment.getImagePost();
                    Comment comment1 = new Comment(customerKey, commentImage, time, commentContent);
                    listComment.add(comment1);
                }
                adapter = new CommentAdapter(listComment);
                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(CommentActivity.this);
                recyclerViewComment.setLayoutManager(mLayoutManager);
                recyclerViewComment.setItemAnimator(new DefaultItemAnimator());
                recyclerViewComment.setAdapter(adapter);
            }
        });
mFirebaseFirestore = FirebaseFirestore.getInstance();
        mCollectionReference = mFirebaseFirestore.collection("Post").document(key).collection("Comment");
        
mCollectionReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                listComment.clear();
                for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots)
                {
                    Comment comment = documentSnapshot.toObject(Comment.class);
                    String commentContent = comment.getCommentContent();
                    String customerKey = comment.getCustomerKey();
                    Date time = comment.getPostTime();
                    String commentImage = comment.getImagePost();
                    Comment comment1 = new Comment(customerKey, commentImage, time, commentContent);
                    listComment.add(comment1);
                }
                adapter = new CommentAdapter(listComment);
                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(CommentActivity.this);
                recyclerViewComment.setLayoutManager(mLayoutManager);
                recyclerViewComment.setItemAnimator(new DefaultItemAnimator());
                recyclerViewComment.setAdapter(adapter);
            }
        });

0 个答案:

没有答案