我正在尝试使用FirebaseRecyclerAdapter
从Firebase获取详细信息来填充recyclerview
。
活动代码如下:-
private FirebaseRecyclerAdapter<UserChat,RecyclerView.ViewHolder> adapter;
final FirebaseRecyclerOptions<UserChat> options = new FirebaseRecyclerOptions.Builder<UserChat>()
.setQuery(queryforDisplayMessages, UserChat.class)
.setLifecycleOwner(this)
.build();
adapter=new FirebaseUserChatAdapter(options);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
mRecyclerView.setAdapter(adapter);
Firebase适配器代码如下
public class FirebaseUserChatAdapter extends FirebaseRecyclerAdapter<UserChat,RecyclerView.ViewHolder> {
private static final String TAG=FirebaseUserChatAdapter.class.getSimpleName();
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
/**
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
* {@link FirebaseRecyclerOptions} for configuration options.
*
* @param options
*/
public FirebaseUserChatAdapter(@NonNull FirebaseRecyclerOptions<UserChat> options) {
super(options);
}
@Override
public int getItemViewType(int position) {
UserChat message = getItem(position);
Log.d(TAG,"usermodel"+message.getUserModel().getId());
if (message.getUserModel().getId().equals("GgGwv7BzOSSoMA5ve2047Neg8aC3")) {
// If the current user is the sender of the message
return VIEW_TYPE_MESSAGE_SENT;
} else {
// If some other user sent the message
return VIEW_TYPE_MESSAGE_RECEIVED;
}
}
@Override
protected void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull UserChat model) {
UserChat message = getItem(position);
switch (holder.getItemViewType()){
case VIEW_TYPE_MESSAGE_SENT:
((FirebaseUserChatViewHolderSent)holder).tvSent.setText(model.getMessage());
case VIEW_TYPE_MESSAGE_RECEIVED:
((FirebaseUserChatViewHolderReceived)holder).tvReceived.setText(model.getMessage());
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
switch (viewType){
case VIEW_TYPE_MESSAGE_SENT:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_user1_item, parent, false);
return new FirebaseUserChatViewHolderSent(view);
case VIEW_TYPE_MESSAGE_RECEIVED:
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_user2_item, parent, false);
return new FirebaseUserChatViewHolderReceived(view);
}
return null;
}
class FirebaseUserChatViewHolderSent extends RecyclerView.ViewHolder {
TextView tvSent;
public FirebaseUserChatViewHolderSent(View itemView) {
super(itemView);
tvSent=itemView.findViewById(R.id.textview_messagesent);
}
}
class FirebaseUserChatViewHolderReceived extends RecyclerView.ViewHolder {
TextView tvReceived;
public FirebaseUserChatViewHolderReceived(View itemView) {
super(itemView);
tvReceived=itemView.findViewById(R.id.textview_messagereceived);
}
}
}
模型类
package com.iworld.daxi.Model;
import java.security.Timestamp;
/**
* Created by Admin on 20/02/2018.
*/
public class UserChat {
private boolean datetimestamp;
private String message;
private String status;
private Object timeStamp;
private UserModel userModel;
public UserChat(){
}
public UserChat(boolean datetimestamp, String message, String status, Object timeStamp, UserModel userModel) {
this.datetimestamp = datetimestamp;
this.message = message;
this.status = status;
this.timeStamp = timeStamp;
this.userModel = userModel;
}
public boolean isDatetimestamp() {
return datetimestamp;
}
public void setDatetimestamp(boolean datetimestamp) {
this.datetimestamp = datetimestamp;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Object timeStamp) {
this.timeStamp = timeStamp;
}
public UserModel getUserModel() {
return userModel;
}
public void setUserModel(UserModel userModel) {
this.userModel = userModel;
}
}
它转到FirebaseUserChatAdapter构造函数。但是没有调用其他方法。所以我的回收器视图没有得到填充。 有人可以帮忙吗?
你只需要在你的onStart()
方法中startListening
protected void onStart() {
super.onStart();
mAdapter.startListening();
}
在你的onStop()
protected void onStop() {
super.onStop();
mAdapter.stopListening();
}
此外,请确保您的 pojo 中的变量与 Firebase 中 datbase 结构中的字段完全匹配。
查看文档
https://github.com/firebase/FirebaseUI-Android/tree/master/firestore/#firestorerecycleradapter-lifecycle
希望对您有所帮助
快乐编码
在此之前:
mRecyclerView.setAdapter(adapter);
添加这个:
adapter.startListening();
希望它会起作用。