gpt4 book ai didi

java - Firebase 的 ListView 聊天消息

转载 作者:行者123 更新时间:2023-12-02 02:00:36 25 4
gpt4 key购买 nike

我创建了一个涉及与其他人聊天的应用程序,为了使聊天 Activity 更加赏心悦目,我创建了一个 ListView 来更改每条消息的 UI。当我发送消息时,第一条消息会出现并正确显示,但如果我发送另一条消息,它会覆盖 listView 中的前一条消息,而不是仅仅向 ListView 添加另一个值,这是我的代码:

聊天 Activity :

private void append_chat_conversation(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Log.e("Chat details: ", String.valueOf(ds));
chat_username = dataSnapshot.child("username").getValue(String.class);
chat_msg = dataSnapshot.child("message").getValue(String.class);

Chat chat = new Chat(chat_username, chat_msg);
ArrayList<Chat> chatList = new ArrayList<>();
chatList.add(chat);

chatListAdapter adapter = new chatListAdapter(this, R.layout.chat_message, chatList);
mListView.setAdapter(adapter);
Log.e("Chat username: ", "" + chat_username);
Log.e("Chat message: ", "" + chat_msg);

}
}

聊天列表适配器:

public class chatListAdapter extends ArrayAdapter<Chat> {

private Context mContext;
int mResource;
TextView usernameTV, messageTV;

public chatListAdapter(Context context, int resource, ArrayList<Chat> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;

}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String username = getItem(position).getUsername();
String message = getItem(position).getMessage();

Chat chat = new Chat(username, message);

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);

usernameTV = convertView.findViewById(R.id.username_chat);
messageTV = convertView.findViewById(R.id.message_chat);

usernameTV.setText(username);
messageTV.setText(message);

return convertView;
}
}

我不会添加其他代码(getter 和 setter 或各个消息的实际布局),因为我认为这不是问题。如果您需要其他任何信息,请告诉我,我确信我在这里做了一些愚蠢的事情来实现这一点,

谢谢,内森

编辑:

我终于找到了如何添加到 ListView 而不用此代码覆盖它:

private void append_chat_conversation(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Log.e("Chat details: ", String.valueOf(ds));
chat_username = dataSnapshot.child("username").getValue(String.class);
chat_msg = dataSnapshot.child("message").getValue(String.class);

Chat chat = new Chat(chat_username, chat_msg);
chatList.add(chat);
Log.e("Chat username: ", "" + chat_username);
Log.e("Chat message: ", "" + chat_msg);
}
adapter.notifyDataSetChanged();
}

数组列表和 adpater 是顶部的公共(public)变量,但它仍然显示该消息两次。我仍然需要找到解决办法。感谢您的帮助

最佳答案

您可以使用 FirebaseListAdapter 显示来自 Firebase 的消息,并对列表中的消息应用自定义布局。

    FirebaseDatabase database = FirebaseDatabase.getInstance();
Query messages = database.getReference("chatrooms").child(sessionId).child("messages");

FirebaseListOptions<Message> options = new FirebaseListOptions.Builder<Message>().setQuery(messages, Message.class).setLayout(R.layout.message).build();

ListView messageList = chatActivity.findViewById(R.id.message_list);
adapter = new FirebaseListAdapter<Message>(options){
@Override
protected void populateView(View v, Message model, int position) {
// Get references to the views of message.xml
TextView messageText = v.findViewById(R.id.message_text);
TextView messageUser = v.findViewById(R.id.message_user);
TextView messageTime = v.findViewById(R.id.message_time);

// Set their text
messageText.setText(model.getMessageBody());
messageUser.setText(model.getUser());

// Format the date before showing it
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
model.getTime()));
}
};

messageList.setAdapter(adapter);
adapter.startListening();

在此处查看更多信息:https://code.tutsplus.com/tutorials/how-to-create-an-android-chat-app-using-firebase--cms-27397

关于java - Firebase 的 ListView 聊天消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51642709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com