gpt4 book ai didi

java - 当我单击recyclerView时如何从Firestore中获取数据?

转载 作者:行者123 更新时间:2023-12-01 16:39:43 25 4
gpt4 key购买 nike

我已经从Firestore检索数据到RecyclerView。当用户单击RecyclerView的任何项目时,他将进入Click Post活动。在点击发布活动中,应显示回收站项目的海报图像和描述。我怎么做?

enter image description here

以下是MainActivity的代码。

public class HomeActivity extends AppCompatActivity {    

private RecyclerView postList;
FirebaseAuth firebaseAuth;
StorageReference storageReference;
FirebaseFirestore db;
DocumentReference documentReference;
private String userid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

firebaseAuth = FirebaseAuth.getInstance();
userid = firebaseAuth.getCurrentUser().getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();
documentReference = db.collection("posts").document();

postList = (RecyclerView) findViewById(R.id.all_user_post_list);
postList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postList.setLayoutManager(linearLayoutManager);}

private void DisplayAllUsersPosts() {

FirebaseFirestore db = FirebaseFirestore.getInstance();
Query query = db.collection("posts");

FirestoreRecyclerOptions<Posts> options = new FirestoreRecyclerOptions.Builder<Posts>()
.setQuery(query, Posts.class)
.build();

FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<Posts, PostsViewHolder>(options) {


@Override
protected void onBindViewHolder(@NonNull PostsViewHolder holder, int position, @NonNull Posts model) {
holder.setTime(model.getTime());
holder.setDate(model.getDate());
holder.setDescription(model.getDescription());
holder.setPostimage(model.getPostimage());
holder.setProfileimage(model.getProfileimage());
holder.setFullname(model.getFullname());


holder.mView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

Intent clickPostIntent = new Intent(HomeActivity.this, ClickPostActivity.class);
clickPostIntent.putExtra("image", model.getPostimage());
clickPostIntent.putExtra("description", model.getDescription());
startActivity(clickPostIntent);
}


});

}

@Override
public PostsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.all_posts_layout, parent, false);

return new PostsViewHolder(view);
}

};
adapter.startListening();
postList.setAdapter(adapter);

}


public static class PostsViewHolder extends RecyclerView.ViewHolder{
View mView;

String userid;

public PostsViewHolder(View itemView){
super(itemView);
mView = itemView;

userid=FirebaseAuth.getInstance().getCurrentUser().getUid();

}


public void setTime(String time){
TextView PostTime = (TextView) mView.findViewById(R.id.post_time);
PostTime.setText(""+ time);
}

public void setDate(String date){
TextView PostDate = (TextView) mView.findViewById(R.id.post_date);
PostDate.setText(" "+date);
}


public void setDescription(String description){
TextView PostDescription = (TextView) mView.findViewById(R.id.click_post_description);
PostDescription.setText(description);
}

public void setPostimage(String postimage){
ImageView PostImage = (ImageView) mView.findViewById(R.id.click_post_image);
Picasso.get().load(postimage).into(PostImage);
}

public void setProfileimage(String profileimage){
CircleImageView image = (CircleImageView) mView.findViewById(R.id.post_profile_image);
Picasso.get().load(profileimage).into(image);
}
public void setFullname(String fullname){
TextView username = (TextView) mView.findViewById(R.id.post_user_name);
username.setText(fullname);
}


}


以下是点击后活动的代码

public class ClickPostActivity extends AppCompatActivity {

private Toolbar mToolbar;
FirebaseAuth firebaseAuth;
StorageReference storageReference;
FirebaseFirestore db;
private String userid;
private ImageView postImage;
private TextView postDescription;
private Button deletePostButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_click_post);

firebaseAuth = FirebaseAuth.getInstance();
userid = firebaseAuth.getCurrentUser().getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();

postImage = (ImageView) findViewById(R.id.click_post_image);
postDescription = (TextView) findViewById(R.id.click_post_description);
deletePostButton = (Button) findViewById(R.id.deletepost);

DocumentReference docRef = db.collection("posts").document();

docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
if(documentSnapshot.exists()){
String postimage = documentSnapshot.getString("postimage");
String description = documentSnapshot.getString("description");

Picasso.get().load(postimage).into(postImage);
postDescription.setText(description);
}
}
});



}


}

最佳答案

在创建新活动之前,获取包含图像和描述的文档,并将它们传递给活动。应该是这样的:

            holder.mView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

DocumentReference docRef = db.collection("posts").document();

docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
if(documentSnapshot.exists()){
String postimage = documentSnapshot.getString("postimage");
String description = documentSnapshot.getString("description");

Intent clickPostIntent = new Intent(HomeActivity.this, ClickPostActivity.class);
clickPostIntent.putExtra("image", postimage);
clickPostIntent.putExtra("description", description);
startActivity(clickPostIntent);
}
}
});
}
});


在ClickPost活动中:

public class ClickPostActivity extends AppCompatActivity {

private Toolbar mToolbar;
FirebaseAuth firebaseAuth;
StorageReference storageReference;
FirebaseFirestore db;
private String userid;
private ImageView postImage;
private TextView postDescription;
private Button deletePostButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_click_post);

firebaseAuth = FirebaseAuth.getInstance();
userid = firebaseAuth.getCurrentUser().getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();

postImage = (ImageView) findViewById(R.id.click_post_image);
postDescription = (TextView) findViewById(R.id.click_post_description);
deletePostButton = (Button) findViewById(R.id.deletepost);

Bundle bundle = getIntent().getExtras();
String image = bundle.getString("image");
String description = bundle.getString("description");

Picasso.get().load(image).into(postImage);
postDescription.setText(description);
}

关于java - 当我单击recyclerView时如何从Firestore中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61885599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com