java - 尝试从博客文章列表中打开单个博客文章视图
问题描述
我正在尝试从使用回收器适配器创建的博客文章列表中打开一篇博客文章。我没有在单个博客帖子活动中使用任何适配器,我正在开发的是一个房屋应用程序,用户可以在其中发布他们的房屋,喜欢和评论他人。我要做的是在选择该帖子时查看单个博客帖子。因此,要使其正常工作,我需要获取所选博客文章的文档 ID,以便我可以从 Firestore DB 中检索它并将其显示在单个博客文章视图中。问题是,我很难检索文档并显示它,因为文档有一个自动生成的密钥。
我最初创建了一个可扩展类,它将文档自动生成的 id 传递给我为将文档发送到回收器适配器而创建的模型类,我想的是使用相同的可扩展类以便将文档 id 传递给单个博客发布视图,我该如何实现?
单篇博文java:
package com.example.android.houseap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.houseap.Listings.HousePost;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
public class SingleHousePost extends AppCompatActivity {
private String mPost_key=null;
private FirebaseFirestore firebaseFirestore;
private TextView locationTextView;
private DocumentReference documentRef;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_house_post);
firebaseFirestore=FirebaseFirestore.getInstance();
firebaseAuth=FirebaseAuth.getInstance();
String house_post_id = String.valueOf(HousePostId.class); //method 1
mPost_key=firebaseFirestore.collection("House_Post").document(house_post_id).getId();
locationTextView=findViewById(R.id.location_city_town);
String currentUser=firebaseAuth.getCurrentUser().getUid();
DocumentReference docRef = firebaseFirestore.collection("House_Posts").document(mPost_key);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
String locationHouse=task.getResult().getString("location");
locationTextView.setText(locationHouse);
} else {
Toast.makeText(SingleHousePost.this, "DATA DOES NOT EXISTS,PLEASE REGISTER", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(SingleHousePost.this, "CANNOT RETRIEVE DATA", Toast.LENGTH_LONG).show();
}
}
});
}
}
Extendable class:
package com.example.android.houseap;
import android.support.annotation.NonNull;
import com.google.firebase.firestore.Exclude;
public class HousePostId {
@Exclude
public String HousePostId;
public<T extends HousePostId>T withId(@NonNull final String id ){
this.HousePostId=id;
return (T) this;
}
}
解决方案
推荐阅读
- printing - 通过结合使用脚本和 Outlook 规则,在 Outlook 中自动打印来自传入电子邮件的附件
- vue.js - 如何捕获一个空的 Vuex Store 状态但在初始化时有一个计算属性对其做出反应
- javascript - 如何在javascript中过滤和创建响应
- css - 使用来自另一个文件的 SCSS 值
- youtube-iframe-api - youtube-iframe-api 显示 iframe 中某些视频的视频不可用
- azure-active-directory - 是否可以通过服务主体/应用注册从 Sql Server Management Studio 连接到 Azure Sql 数据库?
- python - 将 df.iloc 与变量一起使用
- regex - 流畅的解析
- python - Django 错误 PermissionDenied 没有属性 set_cookie
- python - 在 KivyMD 中创建多个 ILeftBodyTouch 项目