【问题标题】:Android Studio Java Firebase retrieve key from CardViewAndroid Studio Java Firebase 从 CardView 检索密钥
【发布时间】: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


【解决方案1】:

一种解决方案是在 ViewHolder 中为 mView 设置 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;
        }

        //...
    }
}

如果你想让它变得更好(这样你就不必处理如何调用 startActivity() 或将 Context 作为参数传递给你的 ViewHolder)你可以用另一个监听器抽象出细节,也许是一个“ListForPostClickedListener”:

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);
    }
}

在您使用 PostsAdapter 的 Activity、Fragment 或任何其他上下文感知对象中,您实现接口并启动必要的 Activity:

public class MyActivityWhichManagesTheAdapter extends Activity implements PostsAdapter.ListForPostClickedListener {

    //...

    public onClick(ListForPost listForPost) {
      //TODO: launch my activity here with mListForPost
    }

}

【讨论】:

  • 嘿,对不起,我没听懂..如何获取活动 1 的数据并将其发送到其他活动?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多