我有一个涉及到消息传递人员的应用程序,每当我从childeventlistener加载一条消息以将其添加到列表视图时,列表视图中的消息/对象就会出现两次,我看不出是什么导致了这种情况?每次发送消息时,我都会调用一个将其添加到适配器的方法,然后使用notifiyDataSetChanged()方法将其添加到列表视图中,就好像它是普通的消息传递应用程序一样。
我的聊天活动:
chatURLRoot.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//append_chat_conversation(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public String chat_msg, chat_username;
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();
}
chatListAdapter:
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;
}
}
非常感谢
内森
答案:
我只是脑子大跳,因为将值添加到列表视图的代码行在for循环中,并且需要检查两个值,所以它将运行该代码行两次!这是更新的代码,我只是把它放在了for循环中:
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);
Log.e("Chat username: ", "" + chat_username);
Log.e("Chat message: ", "" + chat_msg);
}
Chat chat = new Chat(chat_username, chat_msg);
chatList.add(chat);
adapter.notifyDataSetChanged();
}
答案 0 :(得分:0)
这对我有用: 在“我的聊天”活动中,append_chat_conversation已更改。
private void append_chat_conversation(DataSnapshot dataSnapshot) {
chat_username = dataSnapshot.child("username").getValue(String.class);
chat_msg = dataSnapshot.child("message").getValue(String.class);
Chat chat = dataSnapshot.getValue(Chat.class);
adapter.add(chat);
Log.e("Chat username: ", "" + chat_username);
Log.e("Chat message: ", "" + chat_msg);
adapter.notifyDataSetChanged();
}