这个问题困扰我很久了,我一时没想到。我有一个recyclerview设置,我需要它,以便当用户点击recyclerview(帖子)中的项目时,它会从Firebase获取该帖子的文档ID,并将其显示在Toast中或将其存储在变量中
我尝试使用Firestore-UI重写适配器,但无法使其与某些额外的代码一起使用,因此我将其还原。
适配器
public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {
public List<BlogPost> blog_list;
private FirebaseFirestore firebaseFirestore;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
private Context mContext;
public BlogRecyclerAdapter(Context context, List<BlogPost> blog_list) {
mContext = context;
this.blog_list = blog_list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item, parent, false);
firebaseFirestore = FirebaseFirestore.getInstance();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
String desc_data = blog_list.get(position).getDesc();
holder.setDescText(desc_data);
String image_url = blog_list.get(position).getImage_url();
holder.setBlogImage(image_url);
long millisecond = blog_list.get(position).getTimestamp().getTime();
String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString();
holder.setTime(dateString);
// String user_data = blog_list.get(position).getUser_id();
// holder.setUserData(user_data);
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, Confirmation_Activity.class);
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return blog_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public Context mContext;
ConstraintLayout parentLayout;
private View mView;
private TextView descView;
private ImageView blogPostView;
private TextView blogDate;
private TextView blogUserName;
private CircleImageView blogUserImage;
private StorageReference mImageStorage;
@SuppressLint("ResourceType")
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
parentLayout = itemView.findViewById(R.id.Main_Blog_Post);
}
public void setDescText(String descText) {
descView = mView.findViewById(R.id.blog_desc);
descView.setText(descText);
}
public void setUserData(String downloadUrl, String name) {
mImageStorage = FirebaseStorage.getInstance().getReference();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
final String current_uid = mCurrentUser.getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.keepSynced(true);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = Objects.requireNonNull(dataSnapshot.child("name").getValue()).toString();
StorageReference filepath = mImageStorage.child("profile_images").child(current_uid + (".jpeg"));
Log.d("heere", "S");
// This gets the download url async
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//The download url
final String downloadUrl =
uri.toString();
Log.d("tag", downloadUrl);
if (!downloadUrl.equals("default")) {
// Glide.with(getApplicationContext()).load(downloadUrl).into(mDisplayImage);
Glide.with(itemView.getContext()).load(downloadUrl).into(blogUserImage);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
//Toast.makeText(getApplicationContext(), image, Toast.LENGTH_SHORT).show();
Log.d("value", dataSnapshot.getValue().toString());
Log.d("key", dataSnapshot.getKey());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void setTime(String date) {
blogDate = mView.findViewById(R.id.blog_date);
blogDate.setText(date);
}
public void setBlogImage(final String downloadUri) {
blogPostView = mView.findViewById(R.id.blog_image);
Glide.with(itemView.getContext()).load(downloadUri).into(blogPostView);
}
}
}
博客帖子
public class BlogPost {
// No thumb here may cause problems????
public String user_id, image_url, desc;
public Date timestamp;
public BlogPost() {
//empty constructor needed
}
public BlogPost(String user_id, String image_url, String desc, Date timestamp) {
this.user_id = user_id;
this.image_url = image_url;
this.desc = desc;
this.timestamp = timestamp;
}
/*
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
*/
public String getImage_url() {
return image_url;
}
/*
public void setImage_url(String image_url) {
this.image_url = image_url;
}
*/
public String getDesc() {
return desc;
}
/*
public void setDesc(String desc) {
this.desc = desc;
}
*/
public Date getTimestamp() {
return timestamp;
}
/*
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
*/
}
此外,该集合不能硬编码到适配器中,因为如果有问题,我会为帖子提供多个集合。
现在在哪里
博客帖子
public String getPost_id() {
return post_id;
}
//not being used??
public void setPost_id(String post_id) {
this.post_id = post_id;
}
添加了吸气剂和吸气剂
BlogRecyclerAdapter
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = holder.getAdapterPosition();
String id = blog_list.get(pos).getPost_id();
Toast.makeText(mContext, "test" + id, Toast.LENGTH_LONG).show();
Intent intent = new Intent(mContext, Confirmation_Activity.class);
mContext.startActivity(intent);
}
});
当我去运行应用程序并单击一个项目时,吐司面包读取testnull是因为我在文档中没有ID存储作为值吗?
(这是我对应用程序所做的唯一更改)
数据库结构
答案 0 :(得分:1)
我相信您的POJO BlogPost
有一个名为post_id的字段集。
这是一项轻松的任务,在onBindViewHolder
中,执行单击项目时,可以使用该方法提供的位置访问被单击项目的所有属性,您可以这样做
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = blog_list.get(position).getPostID(); //this will be your desired id
//here you can do whatever you want whith this id, pass it to the next activity and perform the request will be the better option.
Intent intent = new Intent(mContext, Confirmation_Activity.class);
mContext.startActivity(intent);
}
});