我正在尝试以聊天形式显示Recyclerview,但是为空,这里找不到代码的问题,我不知道问题在哪里,但是可能在适配器中。
MessageListAdapter.java
public class MessageListAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private Context mContext;
private List<ItemMessage> mMessageList;
public MessageListAdapter(Context context, List<ItemMessage> messageList) {
mContext = context;
mMessageList = messageList;
}
@Override
public int getItemCount() {
return mMessageList.size();
}
// Determines the appropriate ViewType according to the sender of the message.
@Override
public int getItemViewType(int position) {
ItemMessage message = mMessageList.get(position);
if (message.getSender().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
// 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;
}
}
// Inflates the appropriate layout according to the ViewType.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_sent, parent, false);
return new SentMessageHolder(view);
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_received, parent, false);
return new ReceivedMessageHolder(view);
}
return null;
}
// Passes the message object to a ViewHolder so that the contents can be bound to UI.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ItemMessage message = mMessageList.get(position);
switch (holder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_SENT:
((SentMessageHolder) holder).bind(message);
break;
case VIEW_TYPE_MESSAGE_RECEIVED:
((ReceivedMessageHolder) holder).bind(message);
}
}
private class SentMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText;
SentMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
}
void bind(ItemMessage message) {
messageText.setText(message.getMessgae());
// Format the stored timestamp into a readable String using method.
timeText.setText(message.getTime());
}
}
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText, nameText;
ImageView profileImage;
ReceivedMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
nameText = (TextView) itemView.findViewById(R.id.text_message_name);
profileImage = (ImageView) itemView.findViewById(R.id.image_message_profile);
}
void bind(ItemMessage message) {
messageText.setText(message.getMessgae());
// Format the stored timestamp into a readable String using method.
timeText.setText(message.getTime());
nameText.setText("kdkdk");
// Insert the profile image from the URL into the ImageView.
// Utils.displayRoundImageFromUrl(mContext, message.getSender().getProfileUrl(), profileImage);
}
}
}
ItemMessage.java
public class ItemMessage {
private String sender = null;
private String reciever = null;
private String time = null;
private String messgae = null;
public ItemMessage() {
}
public ItemMessage(String sender, String reciever, String time, String messgae) {
this.sender = sender;
this.reciever = reciever;
this.time = time;
this.messgae = messgae;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getReciever() {
return reciever;
}
public void setReciever(String reciever) {
this.reciever = reciever;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getMessgae() {
return messgae;
}
public void setMessgae(String messgae) {
this.messgae = messgae;
}
}
获取代码。
childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ItemMessage itemMessage = dataSnapshot.getValue(ItemMessage.class);
friendlyMessages.add(itemMessage);
mMessageAdapter.notifyDataSetChanged();
Log.d("SSSSz" , friendlyMessages.size()+"");
Log.d("SSSSz", itemMessage.getMessgae());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
databaseReferencemsg.addChildEventListener(childEventListener);
我没有发布其余的代码,因为我知道问题在这里。