我使用了以下依赖项firestore:12.0.1 firebase-ui-firestore:3.3.0
代码编译时没有任何错误,app运行得很好,但recyclelerview没有填充数据。 我曾多次使用类似的代码,但直到现在才遇到任何问题。 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;
}
}
}
答案 0 :(得分:0)
如果您没有使用任何登录方式,例如any of these,那么如果您在firestore
中使用test mode
,则必须在Firestore
中使用lock mode
您的用户必须使用登录方式进行身份验证(如上面的链接)才能访问firestore
中的数据。
只需将您的安全规则更改为
即可service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
再试一次。
答案 1 :(得分:0)
您需要为MessageDetails.class创建一个空的构造函数
遇到一个类似的问题,并通过为主对象的类创建一个空的构造函数来解决该问题。