【发布时间】:2020-09-26 17:45:16
【问题描述】:
目前,我正在制作一个社交媒体应用程序,并且我正在为我的项目使用 firebase firestore 和云存储。有一个评论按钮,如果用户单击该按钮,他应该能够看到每个用户的 cmets 及其姓名和头像。
这是我的firestore数据库结构,
Users(Root collection)
|---- UID1([Document]User ID which generate by authentication)---[Fields-Name,Image,Age]
|---- UID2([Document]User ID which generate by authentication)---[Fields-Name,Image,Age]
Posts(Root collection)
|-----DOCID1(Fields-Post Title,Posted_UID,Post_Image)
|----Comments(Sub-collection)
|----1RANDOMDOCID(Fields-Commented_User_Id,Commented_Date,Comment)
|----2RANDOMDOCID(Fields-Commented_User_Id,Commented_Date,Comment)
好的,我需要将这些 cmets 填充到回收视图中。我将在下面添加我的方法,
Query query = db.collection("Posts").document(CURRENT_SELECTED_DOC_ID).collection("Comments");
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(10)
.setPageSize(20)
.build();
FirestorePagingOptions<CommentsI> options = new FirestorePagingOptions.Builder<CommentsI>()
.setQuery(query, config, CommentsI.class)
.build();
构造函数
public class CommentI {
String Commented_User_Id;
Date Commented_Date;
String Comment;
public CommentI(){
//Empty constructor
}
public class CommentI(String commented_User_Id,Date commented_Date,String comment){
Commented_User_Id = commented_User_Id;
Commented_Date = commented_Date;
Comment = comment;
//------------------------------------------
public String getCommented_User_Id(){
return Commented_User_Id;
}
public Date getCommented_Date(){
return Commented_Date;
}
public String getComment(){
return Comment;
}
这是在 recycleview 中填充项目的常用方法。我可以毫无疑问地获得这些 cmets 和日期,但我需要同时设置用户的图像和名称。这就是我不得不停止工作直到解决这个问题的地方。请有人帮忙:)
编辑
这是我的适配器,当我们尝试像这样检索用户信息时,它总是在上下滚动时读取 firestore 文档。例如 - 当我上下滚动时(想想我在 recyleview 中有 10 个项目),每次从项目 1 滚动到 10 以及从项目 10 滚动到 1 时,我都可以看到名称和图像刷新。如何解决这个问题?
@Override
protected void onBindViewHolder(@NonNull final CommentIViewHolder holder, int position, @NonNull CommentI model) {
holder.comment_textview.setText(model.getComment());
holder.date_textview.setText(DateFormat.format("(yyyy-MM-dd)", model.getCommented_Date()));
//getting UserID-------------
String UserId = model.getCommented_User_Id();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference documentReference = rootRef.collection("Users").document(UserId);
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot doc = task.getResult();
if (doc.exists()) {
String UserName = doc.getString("Name");
String UserImage = doc.getString("Image");
holder.commentor_name.setText(UserName);
holder.setProfile_image_view(UserImage);
}
}
}
});
【问题讨论】:
标签: java android firebase google-cloud-firestore