Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法使用FirbaseUI和Cloud Firestore访问Recyclerview位置

我正在将我的应用程序从Firebase数据库迁移到Firebase Cloud Firestore

之前,我使用FirebaseUI创建实时数据库。初始化所有选项并在实时数据库中创建适配器后,我调用了。在RecyclerView视图上的setOnClickListener()导航到新活动:

holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent toClickedPoll = new Intent(getActivity(), PollHostActivity.class);
                        toClickedPoll.putExtra("POLL_ID", mFireAdapter.getRef(holder.getAdapterPosition()).getKey());
                        startActivity(toClickedPoll);

                    }
                });

我传递的额外信息是Firebase实时数据库中位置的密钥

我正试图通过FirebaseUI访问云Firestore的相同“密钥”。下面是我的代码:

  mFirestoreAdaper = new FirestoreRecyclerAdapter<Poll, PollHolder>(storeOptions) {
            @Override
            protected void onBindViewHolder(@NonNull final PollHolder holder, int position, @NonNull Poll model) {
                holder.mPollQuestion.setText(model.getQuestion());
                String voteCount = String.valueOf(model.getVote_count());
                //TODO: Investigate formatting of vote count for thousands
                holder.mVoteCount.setText(voteCount);
                Picasso.with(getActivity().getApplicationContext())
                        .load(model.getImage_URL())
                        .fit()
                        .into(holder.mPollImage);
                holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent toClickedPoll = new Intent(getActivity(), PollHostActivity.class);
                        String position = String.valueOf(mFirestoreAdaper.getItemId(holder.getAdapterPosition()));
                        Log.v("Firestore ID", position);
                        toClickedPoll.putExtra("POLL_ID", position);
                        startActivity(toClickedPoll);

                    }
                });
            }

现在,位置变量(我已经记录)返回-1。我希望它能返回以下投票文件:

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    试试这个:

       String positions = getSnapshots().getSnapshot(position).getId();
       Log.v("Firestore ID", positions);
       toClickedPoll.putExtra("POLL_ID", positions);
    

    而不是这个:

        String position = String.valueOf(mFirestoreAdaper.getItemId(holder.getAdapterPosition()));
                        Log.v("Firestore ID", position);
                        toClickedPoll.putExtra("POLL_ID", position);