【发布时间】:2018-04-15 18:33:46
【问题描述】:
所以我一直在整个互联网上寻找解决方案,但遗憾的是没有一个对我有用。 我的目标是从存储在 Firebase 上的 cardView 中检索数据,并在单击 cardView 时使用该数据以将其显示在新的 Activity 上。
我已将“帖子”创建为集合,因此标题、日期、图像等所有数据都存储在“帖子”集合中。
据我了解,我需要做的是从已点击的 cardView 数据中检索密钥,并使用它使用相同的互密钥在新活动上实现相同的数据。
我被困在这里很久了,我很想得到一些帮助..非常感谢!
我的代码 -
RecyclerAdapter -
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.ViewHolder> {
public List<ListForPost> list_post;
public Context context;
public PostsAdapter(List<ListForPost> list_post) {
this.list_post = list_post;
}
@NonNull
@Override
public PostsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts_intro, parent, false);
context = parent.getContext();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final PostsAdapter.ViewHolder holder, int position) {
String header_data = list_post.get(position).getHeader();
holder.setHeaderText(header_data);
String date_data = list_post.get(position).getDate1();
holder.setDateText(date_data);
String image_data = list_post.get(position).getImage_url();
holder.setIntroIMG(image_data);
}
@Override
public int getItemCount() {
return list_post.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private View mView;
private ImageView introIMG;
private TextView headerText;
private TextView dateText;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setHeaderText(String headText) {
headerText = mView.findViewById(R.id.introHeader);
headerText.setText(headText);
}
public void setDateText(String tarihText) {
dateText = mView.findViewById(R.id.introDate);
dateText.setText(tarihText);
}
public void setIntroIMG (String downloadUri) {
introIMG = (ImageView) mView.findViewById(R.id.introImage);
Glide.with(context).load(downloadUri).into(introIMG);
}
}
}
MainActivity -
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("Posts").limit(10).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
ListForPost listForPost = doc.getDocument().toObject(ListForPost.class);
list_post.add(listForPost);
postsAdapter.notifyDataSetChanged();
}
}
}
});
【问题讨论】:
-
只需点击添加查询!并根据收到的数据打开其他活动
-
你能举个例子吗?我的问题是发送和接收相同的数据.. 打开新活动不是困难的部分
-
发布你的节点结构!
-
您可以做两件事,首先获取所有帖子数据,然后在您的卡片视图中仅使用标题,当您点击您的回收视图卡片时获取该对象并将其发送到其他活动
标签: java firebase android-recyclerview key google-cloud-firestore