java - 无法同时检索内容和 postId
问题描述
我设法在我的回收站视图中显示用户的帖子。现在我想通过点击在另一个活动上打开这篇文章。然后我希望能够对这篇文章发表评论。
所以我试图编写这个程序。
我的代码中的问题是,当我尝试将 PostId 指向活动时,帖子的内容不再发送到此活动,因此活动上不会显示任何内容。此代码无法将帖子 ID 和帖子内容发送到我的活动(请参阅代码中的注释)。
我应该如何设计可以接收帖子内容和帖子ID的代码?
这是我的适配器:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder>{
List<Post> list;
Activity context;
FirebaseFirestore firebaseFirestore;
FirebaseAuth firebaseAuth;
public PostAdapter(Activity context, List<Post> list){
this.list = list;
this.context = context;
}
public void initViews(){
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.each_post, parent, false);
initViews();
return new PostViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
Post post = list.get(position);
holder.setpostContent(post.getContent());
long milliseconds = post.getTime().getTime();
String date = DateFormat.format("MM/dd/yyyy", new Date(milliseconds)).toString();
holder.setDate(date);
String userId = post.getUser();
firebaseFirestore.collection("Users").document(userId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
String user = task.getResult().getString("name");
holder.setUser(user);
}else{
Toast.makeText(context, task.getException().toString(), Toast.LENGTH_SHORT).show();
}
}
});
//Here i tried to intent the postId to my new Activity
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, OpenNewPostActivity.class);
String postId = post.PostId;
intent.putExtra("postid", postId);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class PostViewHolder extends RecyclerView.ViewHolder{
public TextView postTime, postContent, username;
View view;
public PostViewHolder(@NonNull View itemView) {
super(itemView);
view = itemView;
view.setOnClickListener(v -> openPost());
}
public void setpostContent(String content){
postContent = view.findViewById(R.id.tv_new_post_text);
postContent.setText(content);
}
public void setDate(String time){
postTime = view.findViewById(R.id.tv_new_post_time);
postTime.setText(time);
}
public void setUser(String user){
username = view.findViewById(R.id.tv_new_post_username);
username.setText(user);
}
public void getConstraintLayout(){
postLayout = view.findViewById(R.id.post_layout);
}
//Here i wanted to sent the content of the post to my new activity so i can display it on my new activity but it is not working.
public void openPost(){
int position = getAdapterPosition();
Bundle bundle = new Bundle();
bundle.putString("content", postContent.getText().toString());
bundle.putString("username", username.getText().toString());
bundle.putString("time", postTime.getText().toString());
Intent intent = new Intent(context, OpenNewPostActivity.class);
intent.putExtras(bundle);
context.startActivity(intent);
}
}
}
这是我的活动
public class OpenNewPostActivity extends AppCompatActivity {
private String postUserName, postTime, postContent;
private TextView userName, time, content;
private EditText comment;
private String postId, currentUser;
private ImageView addComment;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_post);
initViews();
intiClickListener();
setPostContent();
}
private void initViews(){
userName = findViewById(R.id.tv_open_new_post_username);
time = findViewById(R.id.tv_open_new_post_time);
content = findViewById(R.id.tv_open_new_post_text);
addComment = findViewById(R.id.img_open_add_comment);
comment = findViewById(R.id.et_open_comment);
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
currentUser = firebaseAuth.getCurrentUser().getUid();
//Here i receive the post id, so i want to add the comments to the specific post.
postId = getIntent().getStringExtra("postid");
}
private void intiClickListener(){
addComment.setOnClickListener(v -> addNewComment());
}
private void setPostContent(){
// Here i replaced the content, but the textviews are not filled with the content of the post and I don't know why
Bundle bundle = getIntent().getExtras();
postUserName = bundle.getString("username");
postTime = bundle.getString("time");
postContent = bundle.getString("content");
userName.setText(postUserName);
time.setText(postTime);
content.setText(postContent);
}
private void addNewComment(){
String newComment = comment.getText().toString();
if(newComment.isEmpty()){
Toast.makeText(this, "Bitte fügen Sie ein Kommentar hinzu!", Toast.LENGTH_SHORT).show();
}
Map<String, Object> commentMap = new HashMap<>();
commentMap.put("comment", newComment);
commentMap.put("time", FieldValue.serverTimestamp());
commentMap.put("user", currentUser);
firebaseFirestore.collection("Posts/" + postId + "/Comments").add(commentMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if(task.isSuccessful()){
Toast.makeText(OpenNewPostActivity.this, "Kommentar wird hinzugefügt!", Toast.LENGTH_SHORT).show();
comment.setText("");
}else{
Toast.makeText(OpenNewPostActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
解决方案
推荐阅读
- eclipse - Papyrus UML 类符号,入门
- typescript - .d.ts 文件中的自定义类型
- javascript - 打开 html 页面时的类加载
- python - 将字典中的字符串转换为元组
- java - Servlet 中的 Web 应用程序旁边的 Restful Web 服务
- sql - 程序未运行
- python - 将文件名保存在 numpy 文件 (.npy) 中
- python - 每个 PowerShell 调用 Python 脚本并传递 PSObject 并返回解析的数据
- python - 运行这个 django 项目,但它在 manage.py 中向我显示了这个错误
- c# - 登录后无法执行 GET 请求