下面的代码 我似乎已经完成了所需的一切,但回收器视图只是没有被填充。
我什至将登录放在OnDateChange上以查看它是否被调用,并且在Galaxy S6上运行它后,它在LogCat上被调用过一次。此外,数据库是公共的。
RecyclerView chat_view;
EditText message_text;
CollectionReference reference = FirebaseFirestore.getInstance().collection("stage2");
Query query = reference.orderBy("timestamp");
FirestoreRecyclerAdapter<ChatMessage,ChatHolder> adapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//initializing, called below
init();
}
private FirestoreRecyclerAdapter<ChatMessage,ChatHolder> newAdapter() {
final FirestoreRecyclerOptions options = new FirestoreRecyclerOptions.Builder<ChatMessage>()
.setQuery(query,ChatMessage.class)
.setLifecycleOwner(this)
.build();
return new FirestoreRecyclerAdapter<ChatMessage, ChatHolder>(options) {
@Override
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item,parent,false);
//if(((ChatMessage)options.getSnapshots().getSnapshot(viewType)).)
return new ChatHolder(view);
}
@Override
protected void onBindViewHolder(ChatHolder holder, int position, ChatMessage model) {
//displaying the message and users into the recyclerview
holder.user_msg.setText(model.getMessage());
holder.user_date.setText(model.getDate());
holder.user_name.setText(model.getName());
}
@Override
public void onDataChanged() {
Log.d("TAG", "YESSIR");
}
@Override
public void onError(FirebaseFirestoreException e) {
super.onError(e);
}
};
}
private void init(){
chat_view = (RecyclerView) findViewById(R.id.chat_view);
chat_view.setVerticalScrollBarEnabled(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
chat_view.setLayoutManager(layoutManager);
adapter = newAdapter();
chat_view.setAdapter(adapter);
message_text = (EditText) findViewById(R.id.message_text);
message_text.setEnabled(appUtils.isSignedIn());
findViewById(R.id.send_button).setEnabled(appUtils.isSignedIn());
}
所以,我们有它,我不知道此时的问题可能是什么。我也在使用最新的 Firestore UI 依赖项。
看来你已经做了一切必要的事情。使用FirestoreRecycleroptions
并在onCreateViewHolder
中膨胀布局。
为了使FirestoreRecyclerAdapter
能够获得有关活动的数据,您需要侦听onCreate()
之后onStart()
的任何变化。也在onStop
你可以停止听。
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}