Android 当我插入一些数据时,它被插入x次

Android 当我插入一些数据时,它被插入x次,android,android-sqlite,Android,Android Sqlite,我正在开发聊天应用程序。当我发送或检索消息(使用套接字)时,我会将其保存到嵌入式数据库中,然后将其显示给用户。当我第一次开始聊天时,一切都很好。但是如果我重新打开第二个或第三个聊天活动。。。当它插入两个或三个消息时。。。各次。例如: 第一次打开聊天活动一切正常 聊天活动已第三次打开,如果我发送消息,它将克隆为3条消息 聊天活动已打开6次,如果我发送消息,它将克隆为6条消息 我花了两天时间试图解决这个问题,但没有结果。也在网上搜索过,没有结果 以下是聊天室acitvity的代码: sock

我正在开发聊天应用程序。当我发送或检索消息(使用套接字)时,我会将其保存到嵌入式数据库中,然后将其显示给用户。当我第一次开始聊天时,一切都很好。但是如果我重新打开第二个或第三个聊天活动。。。当它插入两个或三个消息时。。。各次。例如:

第一次打开聊天活动一切正常

聊天活动已第三次打开,如果我发送消息,它将克隆为3条消息

聊天活动已打开6次,如果我发送消息,它将克隆为6条消息

我花了两天时间试图解决这个问题,但没有结果。也在网上搜索过,没有结果

以下是聊天室acitvity的代码:

 socket.on("message", new Emitter.Listener() {
            @Override
            public void call(final Object... args) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject data = (JSONObject) args[0];
                        try {
                            String room = data.getString("room");
                            String nickname = data.getString("senderNickname");
                            String message = data.getString("message");
                            String receiverNickname = data.getString("receiverNickname");

                            ContentValues contentValues = new ContentValues();
                            contentValues.put(DbItems.DbValues.MESSAGES_ROOM, room);
                            contentValues.put(DbItems.DbValues.MESSAGE_SENDER, nickname);
                            contentValues.put(DbItems.DbValues.MESSAGE_RECEIVER, receiverNickname);
                            contentValues.put(DbItems.DbValues.MESSAGE, message);
                            database.insert(DbItems.DbValues.TABLE_NAME, null, contentValues);

                            chatBoxAdapter.swapCursor(getAllItems());

                            myRecylerView.setAdapter(chatBoxAdapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
private Cursor getAllItems(){
        return database.query(
                DbItems.DbValues.TABLE_NAME,
                null, null,
                null, null,
                null, null
        );
    }
聊天盒适配器的名称和代码:

@Override
public void onBindViewHolder(final ChatBoxAdapter.MyViewHolder holder, final int position) {
    User user = SharedPrefManager.getInstance(holder.itemView.getContext()).getUser();
    String myEmail = user.getEmail();
    if (!cursor.moveToPosition(position)){
        return;
    }
    String room = cursor.getString(cursor.getColumnIndex(DbItems.DbValues.MESSAGES_ROOM));
    String senderName = cursor.getString(cursor.getColumnIndex(DbItems.DbValues.MESSAGE_SENDER));
    String receiverName = cursor.getString(cursor.getColumnIndex(DbItems.DbValues.MESSAGE_RECEIVER));
    String message = cursor.getString(cursor.getColumnIndex(DbItems.DbValues.MESSAGE));
    if (room.equals(message_room)){
        if (senderName.equals(myEmail)){
            holder.message_right.setText(message);
            holder.message_right.setVisibility(View.VISIBLE);
        }else {
            if (receiverName.equals(myEmail)){
                if (!email.equals(senderName)){
                }else {
                    holder.message_left.setText(message);
                    holder.message_left.setVisibility(View.VISIBLE);
                    holder.message_user_image.setVisibility(View.VISIBLE);
                    String url = "http://192.168.1.111/ulgamda_iuhd/uploads/" + senderName + ".jpg";
                    Picasso.get()
                            .load(url)
                            .placeholder(R.drawable.default_profile)
                            .networkPolicy(NetworkPolicy.NO_CACHE)
                            .memoryPolicy(MemoryPolicy.NO_CACHE)
                            .resize(604, 604)
                            .into(holder.message_user_image);
                }
            }
        }
    }
}
public void swapCursor(Cursor newCursor){
    if (cursor != null){
        cursor.close();
    }
    cursor = newCursor;
    if (newCursor != null){
        notifyDataSetChanged();
    }
}

@迈克,你需要什么样的信息?我们真的不知道。我们不知道您在何处或如何使用这些片段。当你开始整理你的文件时,你会开始看到什么是必要的。@Mike M。好的,我会发布我的完整信息code@MikeM. 添加了更多的代码,当它工作时,我没有任何错误