Android Studio Java Firebase从CardView检索密钥
所以我一直在互联网上寻找解决方案,但遗憾的是没有一个对我有效。 我的目标是从存储在Firebase上的cardView中检索数据,并在单击cardView以在新活动中显示该数据时使用该数据 我已经创建了“Posts”作为一个集合,所以所有的数据,如标题、日期、图像等都存储在“Posts”集合中 据我所知,我需要做的是从已单击的cardView数据中检索密钥,并使用它在新活动上使用相同的公用密钥实现相同的数据 我被困在这里很久了,我很想得到一些帮助。。非常感谢 我的密码- 回收器适配器-Android Studio Java Firebase从CardView检索密钥,java,firebase,android-recyclerview,key,google-cloud-firestore,Java,Firebase,Android Recyclerview,Key,Google Cloud Firestore,所以我一直在互联网上寻找解决方案,但遗憾的是没有一个对我有效。 我的目标是从存储在Firebase上的cardView中检索数据,并在单击cardView以在新活动中显示该数据时使用该数据 我已经创建了“Posts”作为一个集合,所以所有的数据,如标题、日期、图像等都存储在“Posts”集合中 据我所知,我需要做的是从已单击的cardView数据中检索密钥,并使用它在新活动上使用相同的公用密钥实现相同的数据 我被困在这里很久了,我很想得到一些帮助。。非常感谢 我的密码- 回收器适配器- publ
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);
}
}
public类PostsAdapter扩展了RecyclerView.Adapter{
公开名单;;
公共语境;
公共邮递员(名单/邮递员){
this.list\u post=list\u post;
}
@非空
@凌驾
public PostsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int viewType){
View-View=LayoutInflater.from(parent.getContext()).flate(R.layout.posts\u intro,parent,false);
context=parent.getContext();
返回新的ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull final PostsAdapter.ViewHolder,int位置){
String header_data=list_post.get(position.getHeader();
holder.setHeaderText(表头数据);
字符串date\u data=list\u post.get(position.getDate1();
holder.setDateText(日期数据);
字符串image_data=list_post.get(position.getImage_url();
holder.setIntroIMG(图像数据);
}
@凌驾
public int getItemCount(){
返回列表_post.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
私有视图;
私有图像视图介绍;
私有文本视图标题文本;
私有文本查看日期文本;
公共视图持有者(视图项视图){
超级(项目视图);
mView=项目视图;
}
公共无效setHeaderText(字符串HeaderText){
headerText=mView.findviewbyd(R.id.introHeader);
headerText.setText(headerText);
}
公共void setDateText(字符串tarihText){
dateText=mView.findviewbyd(R.id.introDate);
dateText.setText(tarihText);
}
public void setIntroIMG(字符串下载URI){
introIMG=(ImageView)mView.findviewbyd(R.id.introImage);
Glide.with(context.load)(downloadUri.into)(introIMG);
}
}
}
主要活动-
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();
}
}
}
});
firebaseFirestore=firebaseFirestore.getInstance();
firebaseFirestore.collection(“Posts”).limit(10).addSnapshotListener(新的EventListener(){
@凌驾
public void OneEvent(QuerySnapshot queryDocumentSnapshots,FirebaseFirestoreException e){
对于(DocumentChange文档:queryDocumentSnapshots.getDocumentChanges()){
if(doc.getType()==DocumentChange.Type.ADDED){
ListForPost ListForPost=doc.getDocument().toObject(ListForPost.class);
list_post.add(listForPost);
postsAdapter.notifyDataSetChanged();
}
}
}
});
一种解决方案是在您的ViewHolder内部为MVView设置OnClickListener。因此,对于您的代码,应该是这样的:
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.ViewHolder> {
//...
public class ViewHolder extends RecyclerView.ViewHolder {
private View mView;
private ListForPost mListForPost;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mView.setOnClickListener(new OnClickListener() {
@Override
public void onClick() {
//TODO: launch my activity here with mListForPost
}
});
}
private void setListForPost(ListForPost listForPost) {
mListForPost = listForPost;
}
//...
}
}
只需单击添加查询。!关于收到的数据,请打开其他活动,你能给我举个例子吗?我的问题是发送和接收相同的数据。。打开新活动并不是发布节点结构的难点。!你可以做两件事,首先获取所有帖子数据,然后在你的cardview中只使用标题,当你点击你的recycleview卡时,获取该对象并将其发送到其他活动嘿,对不起,我不明白。。如何获取活动1的数据并将其发送到其他活动?
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.ViewHolder> {
//...
private ListForPostClickedListener mListForPostClickedListener;
public PostsAdapter(ListForPostClickedListener listForPostClickedListener) {
mListForPostClickedListener = listForPostClickedListener;
}
public class ViewHolder extends RecyclerView.ViewHolder {
//...
public ViewHolder(View itemView) {
//...
mView.setOnClickListener(new OnClickListener() {
@Override
public void onClick() {
mListForPostClickedListener.onClick(mListForPost);
}
});
}
//...
}
public interface ListForPostClickedListener {
void onClick(ListForPost listForPost);
}
}
public class MyActivityWhichManagesTheAdapter extends Activity implements PostsAdapter.ListForPostClickedListener {
//...
public onClick(ListForPost listForPost) {
//TODO: launch my activity here with mListForPost
}
}