我正在制作一个Android应用程序,我需要一些帮助。我将4件事保存到数据存储类:字符串(聊天名称),字符串列表(完整聊天),chatmessages列表(汇总聊天)和整数(汇总金额)。数据存储类用于节省和加载聊天,除了完整聊天的加载外,一切都可以使用。我将在下面附加代码和日志输出。

问题描述 投票:0回答:0
MainActivity.kt

private fun initSavedChatsMenu() { setupButton(R.id.btAdventure1) { switchLayout(LayoutType.CHAT) } setupButton(R.id.btSettings1) { switchLayout(LayoutType.SETTINGS) } val recyclerView = findViewById<RecyclerView>(R.id.recyclerViewOfSavedChats) val buttonLabels = mutableListOf<String>() // Populate button labels from ChatStorage for (i in 0 until ChatStorage.getSize()) { buttonLabels.add(ChatStorage.getName(i)) } // Initialize the adapter with the labels and a callback val buttonAdapter = ButtonAdapter(buttonLabels) { label -> // Handle button clicks here // For example, switch to the chat layout switchLayout(LayoutType.CHAT) initChatMenu(true, label) } // Set up RecyclerView with the adapter and layout manager recyclerView.adapter = buttonAdapter recyclerView.layoutManager = LinearLayoutManager(this) } // Initialize the buttons in the Chat layout (ChatGPT[3]) private fun initChatMenu(load: Boolean = false, name: String = "") { setupButton(R.id.btSavedChats2) { switchLayout(LayoutType.SAVED_CHATS) } setupButton(R.id.btSettings2) { switchLayout(LayoutType.SETTINGS) } conversation.clear() summarized.clear() // Set up RecyclerView and Adapter val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) val chatAdapter = ChatAdapter(conversation) // Use ChatAdapter directly recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = chatAdapter // Reference to TextInputEditText val inputEditText = findViewById<TextInputEditText>(R.id.textInputEditText) if (!load) { //irrelevant, the code block that was here handles new chat initialization } } else { // **Load saved conversation** val chatIndex = ChatStorage.getIDByName(name) ?: return conversation.clear() val tempConversation = ChatStorage.getText(chatIndex) summarized = ChatStorage.getSummarized(chatIndex) summarizedAmt = ChatStorage.getSummarizedAmount(chatIndex) // Notify adapter after adding all messages for (i in 0 until conversation.size) { conversation.add(tempConversation[i]) chatAdapter.notifyItemInserted(i) recyclerView.smoothScrollToPosition(conversation.size - 1) } } //also irrelevant, the code block that was here handles new chat input }

输出量:

WRLD - Adding name: istfg if this doesn't workWRLD - Adding text: [Greetings, adventurer! ...] WRLD - Adding summarized: [ChatMessage(role=Role(role=system), messageContent=TextContent(content=Commit this message to memory. ...] WRLD - Adding summarized amount: 0 WRLD - Save Complete. //the above five logs appear when a chat is saved. WRLD - Chat storage size: 1 WRLD - Getting name at index: 0. Name: istfg if this doesn't work //the above two logs appear when the saved chats menu is loaded. WRLD - Getting ID by name: istfg if this doesn't work WRLD - Getting text at index: 0. Text: [] WRLD - Getting summarized at index: 0. Summarized: [ChatMessage(role=Role(role=user), messageContent=TextContent(content=Commit this message to memory. ...] WRLD - Getting summarized amount at index: 0. Summarized amount: 0 //the above four messages appear when a chat is loaded. //as you can see, the "text at index: 0" is empty. the code that handle this manipulation of data is the exact same as that for the summarized version, except for the data type. what could be the cause of this issue? 任何人都对为什么会发生这种情况有任何想法吗?

	
Reddit上的u/四面体的解答:

My hunch is that you are passing the text back as a MutableList somewhere and calling .clear() on it without realizing that it’s a reference to the storage. Modify getText and addText to copy into a new array and see if the problem goes away. 当我将其作为参数传递时,我正在为Mutablelist创建一个别名。当我在其上打电话给.crear()时,列表在两个文件中均已删除。


android kotlin android-studio save mutablelist
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.