作者:xeyuxing369 | 来源:互联网 | 2023-07-29 10:41
我正在Android Studio上使用Firebase数据库。代码Java。我使用实时数据库。
我正在做一个聊天应用。我想向用户显示收件箱中的最后一封邮件。
我有这个数据库:
message: {
User uıd: {
Uid of the person he is talking to: {
random key: {
-date
-from
-message
-time
-type
}
}
}
我引用了数据库als:
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getcurrentUser().getUid();
Chatsref=FirebaseDatabase.getInstance().getReference().child("message").child(currentUserID);
我正在使用它来获取:
public void messageegetir(){
FirebaseRecyclerOptions optiOns=
new FirebaseRecyclerOptions.Builder()
.setQuery(Chatsref,Messages.class)
.build();
FirebaseRecyclerAdapter adapter =
new FirebaseRecyclerAdapter(options) {
@Override
protected void onBindViewHolder(@NonNull final ChatsViewHolder holder,final int position,@NonNull final Messages model) {
final String usersIDs = getRef(position).getKey();
final String[] retImage = {"default_image"};
}
@NonNull
@Override
public ChatsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup,int i) {
View view = LayoutInflater.from(viewGroup.getcontext()).inflate(R.layout.message_list_model,viewGroup,false);
return new ChatsViewHolder(view);
}
};
chatlist.setadapter(adapter);
adapter.startListening();
updateUserStatus("online");
}
我的公共统计类:
public static class ChatsViewHolder extends RecyclerView.ViewHolder{
CircleImageView profileImage ;
ImageView onlinestatus;
TextView lastmessage,username ;
public ChatsViewHolder(@NonNull View itemView) {
super(itemView);
profileImage =itemView.findViewById(R.id.messageprofileimageee);
username =itemView.findViewById(R.id.messagenameee);
lastmessage =itemView.findViewById(R.id.messageinfooo);
Onlinestatus=itemView.findViewById(R.id.onlinestatus);
}
}
消息类:
public class Messages {
public String data,time,type,message,from ;
public Messages(){
}
public Messages(String data,String time,String type,String message,String from) {
this.data = data;
this.time = time;
this.type = type;
this.message = message;
this.from = from;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getFrom() {
return from;
}
public void setfrom(String from) {
this.from = from;
}
}
如何获取这些数据并将其写入textview?
我建议您修改数据库结构,像Firebase文档建议的那样对它进行规范化。查看链接https://firebase.google.com/docs/database/android/structure-data#best_practices_for_data_structure
您将了解
,
我做了一个聊天应用程序,我这样做的方式是我有两个主要根,一个专门用于获取最后发送的消息,每次收到新消息时都会更新,另一个根专门用于存储两个用户之间所有消息的列表,所以我建议做一些类似的事情。