【问题标题】:FirestoreRecyclerOptions no documents retrieved in recyclerviewFirestoreRecyclerOptions 没有在 recyclerview 中检索到文档
【发布时间】:2018-04-01 09:38:14
【问题描述】:

我使用了以下依赖项 firestore:12.0.1 firebase-ui-firestore:3.3.0

代码编译没有任何错误,应用程序运行良好,但 recyclerview 没有填充数据。 我以前多次使用过类似的代码,但到目前为止从未遇到任何问题。 firebase-ui 可能是原因吗?任何帮助表示赞赏。

public class ChatActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView messageList;

    FirebaseFirestore fireShop = FirebaseFirestore.getInstance();
    private FirestoreRecyclerAdapter firestoreRecyclerAdapter;
    private FirebaseAuth firebaseAuth;
    private ImageButton imageButton;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        firebaseAuth = FirebaseAuth.getInstance();
        imageButton = (ImageButton) findViewById(R.id.button_chatbox_send);
        editText = (EditText) findViewById(R.id.edittext_chatbox) ;
        imageButton.setOnClickListener(this);


            messageList = (RecyclerView) findViewById(R.id.reyclerview_message_list);
            messageList.setHasFixedSize(true);
            messageList.setLayoutManager(new LinearLayoutManager(this));
            fetchData();




    }
    public class MessageViewHolder extends RecyclerView.ViewHolder{
        View mView;

        public MessageViewHolder(View itemView) {
            super(itemView);
            mView=itemView;
        }
        public void setMess(String messs){

            TextView messageView = (TextView) mView.findViewById(R.id.text_message_body);
            messageView.setText(messs);
        }

    }


    @Override
    public void onClick(View v) {
        if (v == imageButton){
            addData();

        }
    }
    public void fetchData( ){


        Query query=fireShop.collection("chat");
        Log.d("SGGGGG",query.toString());

        FirestoreRecyclerOptions<MessageDetails> details = new FirestoreRecyclerOptions.Builder<MessageDetails>()
                .setQuery(query,MessageDetails.class)
                .build();
        Log.d("ffffffffffFFRRRRRRRRRG",details.getSnapshots().toString());
        firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<MessageDetails, MessageViewHolder>(details) {

            @Override
            protected void onBindViewHolder(@NonNull MessageViewHolder holder, int position, @NonNull MessageDetails model) {
                Log.d("FFFFFFFFFFFRRRRRRRRRG",model.getMessage());

                holder.setMess(model.getMessage());


            }

            @Override
            public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_message_sent,parent,false);
                return new MessageViewHolder(view);
            }

            @Override
            public void onError(FirebaseFirestoreException e) {
                Log.e("error", e.getMessage());
            }


        };
        messageList.setAdapter(firestoreRecyclerAdapter);

        firestoreRecyclerAdapter.notifyDataSetChanged();




    }

   @Override
    public void onStop() {
        super.onStop();
        firestoreRecyclerAdapter.stopListening();
    }
    @Override
    public void onStart() {
        super.onStart();
        firestoreRecyclerAdapter.startListening();
    }



}

Data Screenshot

安全规则

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

【问题讨论】:

  • 尝试删除setHasFixedSize
  • 试过了,但没有改变。
  • 安全规则呢?另外,你的数据截图?
  • 安全规则:
  • 如果您有兴趣,this 是您可以从 Cloud Firestore 数据库中检索数据并使用 FirestoreRecyclerAdapter 将其显示在 RecyclerView 中的方法。如果您有任何问题,请告诉我。

标签: android google-cloud-firestore firebaseui


【解决方案1】:

如果您没有使用任何登录方法,例如any of these,那么您必须在test mode 中使用firestore,如果您在lock mode 中使用Firestore,那么您的用户必须使用登录方法进行身份验证(如上面的链接)从firestore访问数据。

只需将您的安全规则更改为

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

然后再试一次。

【讨论】:

  • 使用电子邮件登录。用户会话处于活动状态。
  • 你的Log.d("FFFFFFFFFFFRRRRRRRRRG",model.getMessage()); 打印的是什么??
  • 你的代码很完美。它在这里打印任何东西吗Log.e("error", e.getMessage());
  • 那里也没有
【解决方案2】:

你需要为MessageDetails.class创建一个空的构造函数

刚刚遇到了类似的问题,通过为主对象的类创建一个空的构造函数来解决它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2020-08-16
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多