我尝试制作一对一的聊天应用,并且我有带有孩子的firebase数据库(receiver_sender)。我使用FirebaseRecyclerAdapter来显示消息,但它不起作用。向firebase发送消息是有效的,但接收和显示消息并不起作用。任何人都可以帮忙吗?
这是我的代码:
public class MessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
room_type_1 = recipiant + "_" + sender;
room_type_2 = sender + "_" + recipiant;
// New child entries
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference("messages");
mFirebaseDatabaseReference
.getRef()
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(room_type_1)) {
listFirebase(room_type_1);
} else if (dataSnapshot.hasChild(room_type_2)) {
listFirebase(room_type_2);
} else {
Log.e(TAG, "getMessageFromFirebaseUser: no such room available");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Unable to get message
}
});
public void listFirebase(String room){
mFirebaseAdapter = new FirebaseRecyclerAdapter<FriendlyMessage,MessageViewHolder>(
FriendlyMessage.class,
R.layout.item_message,
MessageViewHolder.class,
mFirebaseDatabaseReference.child(room)) {
@Override
protected void populateViewHolder(final MessageViewHolder viewHolder,
FriendlyMessage friendlyMessage, int position) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
if (friendlyMessage.getText() != null) {
viewHolder.messageTextView.setText(friendlyMessage.getText());
viewHolder.messageTextView.setVisibility(TextView.VISIBLE);
viewHolder.messageImageView.setVisibility(ImageView.GONE);
} else {
String imageUrl = friendlyMessage.getImageUrl();
if (imageUrl.startsWith("gs://")) {
StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl(imageUrl);
storageReference.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
String downloadUrl = task.getResult().toString();
Glide.with(viewHolder.messageImageView.getContext())
.load(downloadUrl)
.into(viewHolder.messageImageView);
} else {
Log.w(TAG, "Getting download url was not successful.",
task.getException());
}
}
});
} else {
Glide.with(viewHolder.messageImageView.getContext())
.load(friendlyMessage.getImageUrl())
.into(viewHolder.messageImageView);
}
viewHolder.messageImageView.setVisibility(ImageView.VISIBLE);
viewHolder.messageTextView.setVisibility(TextView.GONE);
}
viewHolder.messengerTextView.setText(friendlyMessage.getName());
if (friendlyMessage.getPhotoUrl() == null) {
viewHolder.messengerImageView.setImageDrawable(ContextCompat.getDrawable(MessageActivity.this,
R.drawable.ic_account_circle_black_36dp));
} else {
Glide.with(MessageActivity.this)
.load(friendlyMessage.getPhotoUrl())
.into(viewHolder.messengerImageView);
}
}
};
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = mFirebaseAdapter.getItemCount();
int lastVisiblePosition =
mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the
// user is at the bottom of the list, scroll to the bottom
// of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
mMessageRecyclerView.scrollToPosition(positionStart);
}
}
});
}
}
和我的数据库:
{
"messages" : {
"ficika3_c" : {
"1489872857484" : {
"name" : "c",
"recipient" : "ficika3",
"sender" : "c",
"text" : "gghjj",
"time" : "1489872857484"
},
"1489872859060" : {
"name" : "c",
"recipient" : "ficika3",
"sender" : "c",
"text" : "hhh",
"time" : "1489872859060"
},
"1489872900098" : {
"name" : "ficika3",
"recipient" : "c",
"sender" : "ficika3",
"text" : "gghh",
"time" : "1489872900098"
},
"1489873714959" : {
"name" : "ficika3",
"recipient" : "c",
"sender" : "ficika3",
"text" : "gg",
"time" : "1489873714959"
}
},
"rokvic_ficika3" : {
"1489872702555" : {
"name" : "ficika3",
"recipient" : "rokvic",
"sender" : "ficika3",
"text" : "gghh",
"time" : "1489872702555"
},
"1489872730698" : {
"name" : "ficika3",
"recipient" : "rokvic",
"sender" : "ficika3",
"text" : "zhj",
"time" : "1489872730698"
}
}
}