我已经使用了以下依赖项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();
}
}
数据屏幕截图
安全规则
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
如果您不使用任何类似的登录方法,则必须在test mode
中使用firestore
,如果您在lock mode
中使用Firestore
,则必须使用登录方法对您的用户进行身份验证(如上链接中(从firestore
访问数据。
只需将您的安全规则更改为
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
然后重试。
您需要为MessageTails.class
创建一个空的构造函数只是有类似的问题,并通过为主对象的类创建一个空构造函数解决了问题。