我使用带有节点后端的聊天程序,我使用socket.io,服务器接受数据并正确发送。
但是当我仅发送消息或仅接收消息时,最后消息内容会像上一条消息内容一样发生变化。例如第一条消息:“dog”,第二条消息:“cat”当我按下发送第二条消息时第一条消息和所有先前消息(如果存在)变为“cat”。
如果我尝试发送和接收消息,列表视图中的消息将逐一消失,因为我一直在发送消息。例如,如果列表中有10条消息从我发送,当一条消息来自服务器时,列表中的消息变为9,然后是8,然后是7,等等,直到它为空。
我检查了列表适配器中的ArrayList并且它正常工作,我认为问题出在MessageListAdapter.java中的getView()方法中。
activity_chat.xml
<ListView
android:id="@+id/chat_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/chat_activity_line"
android:layout_marginBottom="70dp"
android:divider="@android:color/transparent"
android:dividerHeight="20.0sp"
android:stackFromBottom="true"
app:layout_constraintBottom_toTopOf="@+id/chat_activity_line"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<View
android:id="@+id/chat_activity_line"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginBottom="0dp"
android:background="#dfdfdf"
app:layout_constraintBottom_toTopOf="@+id/layout_chatbox"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<RelativeLayout
android:id="@+id/layout_chatbox"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<Button
android:id="@+id/button"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="53dp"
android:background="@drawable/hide_border"
android:onClick="activity_chat_send_message_btn"
android:text="send"
tools:layout_editor_absoluteX="144dp"
tools:layout_editor_absoluteY="516dp" />
<EditText
android:id="@+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="@drawable/custom_chat_edittext"
android:ems="10"
android:hint="@string/ChatActivity_chat_box_hint"
android:inputType="textPersonName"
android:minHeight="35dp"
tools:layout_editor_absoluteX="197dp"
tools:layout_editor_absoluteY="552dp" />
</RelativeLayout>
list_chat.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/chat_message_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="25dp"
android:layout_marginTop="14dp"
android:background="@drawable/custom_chat_other"
android:fontFamily="@font/cairo"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="Hello Left"
android:textColor="@color/black" />
<TextView
android:id="@+id/chat_message_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/chat_message_other"
android:layout_alignBottom="@+id/chat_message_other"
android:layout_alignParentEnd="true"
android:layout_marginEnd="25dp"
android:background="@drawable/custom_chat_me"
android:fontFamily="@font/cairo"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="Hello Right"
android:textColor="@color/white" />
Message.java
public class Message {
public String mssg, sender ;
public Message(){}
public Message(String mssg, String sender) {
this.mssg = mssg;
this.sender = sender;
}
public String getMssg() {
return mssg;
}
public void setMssg(String mssg) {
this.mssg = mssg;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
}
MessageListAdapter.java
public class MessageListAdapter extends ArrayAdapter {
List<Message> list = new ArrayList<Message>();
public MessageListAdapter(@NonNull Context context, int resource) {
super(context, resource);
}
public void add(Message object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return list.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Message messgae = (Message) getItem(position);
View row = convertView;
ChatHolder chatHolder;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_chat, parent, false);
chatHolder = new ChatHolder();
row.setTag(chatHolder);
}
else chatHolder = (ChatHolder) row.getTag();
// linking views
chatHolder.mssg_me = (TextView) row.findViewById(R.id.chat_message_me);
chatHolder.mssg_other = (TextView) row.findViewById(R.id.chat_message_other);
// condition for me and other
if (messgae.getSender().equals("me")) {
chatHolder.mssg_me.setText(list.get(list.size()-1).getMssg());
chatHolder.mssg_other.setVisibility(View.GONE);
}else if (messgae.getSender().equals("other")) {
chatHolder.mssg_other.setText(list.get(list.size()-1).getMssg());
chatHolder.mssg_me.setVisibility(View.GONE);
}
return row;
}
static class ChatHolder {
TextView mssg_me;
TextView mssg_other;
}
}
聊天Activity.java
private void send_newMessage_to_node(String mssg) {
Message message = new Message(mssg, "me");
chatListAdapter.add(message);
chatListAdapter.notifyDataSetChanged();
// send to node server
mSocket.emit("message", mssg);
}
private void addMessageFromNodeToListview(String mssg) {
Message message = new Message(mssg, "other");
chatListAdapter.add(message);
chatListAdapter.notifyDataSetChanged();
}
答案 0 :(得分:0)
答案是:
1-当我发送或接收消息时,将所有列表项内容更改为最后一项的问题我必须将list.get(list.size()-1)
更改为list.get(position)
,因为这会更改所有列表项。
2-对于每次发送或接收项目时删除项目的问题,解决方案在if-else中,如果在getView()中,我需要重置其他TextView上的可见性。也就是说,在if块中,chatHolder.mssg_me.setVisibility(View.VISIBLE);以及else if if块中的mssg_other类似。
回答