我在代码中使用FirestoreRecyclerAdapter进行查询,但构造函数失败。 使用日志,我看到了查询功能。
我将在回收站视图中使用它。
此处是适配器的代码: 公共类MentorChatAdapter扩展了FirestoreRecyclerAdapter {
public interface Listener {
void onDataChanged();
}
//FOR DATA
private final RequestManager glide;
private final String idCurrentUser;
//FOR COMMUNICATION
private Listener callback;
public MentorChatAdapter(@NonNull FirestoreRecyclerOptions<Message> options, RequestManager glide, Listener callback, String idCurrentUser) {
super(options);
Log.e("TAG","adapter try to create");
this.glide = glide;
Log.e("TAG","adapter glide");
this.callback = callback;
Log.e("TAG","adapter callback");
this.idCurrentUser = idCurrentUser;
Log.e("TAG","adapter id");
}
@Override
protected void onBindViewHolder(@NonNull MessageViewHolder holder, int position, @NonNull Message model) {
holder.updateWithMessage(model, this.idCurrentUser, this.glide);
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MessageViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_forum_chat_item, parent, false));
}
@Override
public void onDataChanged() {
super.onDataChanged();
this.callback.onDataChanged();
}
}
这里是我称为适配器的代码,我在我的活动的OnCreate中将其称为:
private void configureRecyclerView(String chatName){
Log.e("TAG","recycler call");
//Track current chat name
this.currentChatName = chatName;
Log.e("TAG","step1 ok");
//Configure Adapter & RecyclerView
this.mentorChatAdapter = new MentorChatAdapter(generateOptionsForAdapter(MessageHelper.getAllMessageForChat(this.currentChatName)), Glide.with(this), this, FirebaseAuth.getInstance().getCurrentUser().getUid());
mentorChatAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
recyclerView.smoothScrollToPosition(mentorChatAdapter.getItemCount()); // Scroll to bottom on new messages
}
});
Log.e("TAG","step2");
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Log.e("TAG","Step3");
recyclerView.setAdapter(this.mentorChatAdapter);
Log.e("TAG","Step4");
}
// 6 - Create options for RecyclerView from a Query
private FirestoreRecyclerOptions<Message> generateOptionsForAdapter(Query query){
return new FirestoreRecyclerOptions.Builder<Message>()
.setQuery(query, Message.class)
.setLifecycleOwner(this)
.build();
}
这里的查询代码我认为没有错误:
public static Query getAllMessageForChat(String chat){
Query q = ChatHelper.getChatCollection()
.document(chat)
.collection(COLLECTION_NAME)
.orderBy("dateCreated")
.limit(50);
Log.e("TAG","getAllMessageForChat : "+q);
return q;
}
这里是logcat(我只能输入png,因为logcat自动刷新):logcat