gpt4 book ai didi

java - 更新数组列表中的特定项目

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:37 25 4
gpt4 key购买 nike

我想更新数组列表中的特定项目。

这是对话类:

class Conversation
{
String sender,to,name,bio,picture;
Integer id,time,unread;
public Conversation() {

}
public Conversation (int id,String sender,String to,String name,String bio,String picture,int time,int unread) {
this.sender=sender;
this.to=to;
this.id=id;
this.name=name;
this.bio=bio;
this.picture=picture;
this.time=time;
this.unread=unread;
}

public void setSender(String sender) {
this.sender=sender;
}
public void setTo(String to) {
this.to=to;
}
public void setId(int id) {
this.id=id;
}
public void setTime(int time) {
this.time=time;
}
public void setUnread(int unread) {
this.unread=unread;
}
public void setName(String name) {
this.name=name;
}
public void setBio(String bio) {
this.bio=bio;
}
public void setPicture(String picture) {
this.picture=picture;
}

public String getSender() {
return this.sender;
}
public String getTo() {
return this.to;
}
public int getId() {
return this.id;
}
public int getTime() {
return this.time;
}
public int getUnread() {
return this.unread;
}
public String getName() {
return this.name;
}
public String getBio() {
return this.bio;
}
public String getPicture() {
return this.picture;
}
}

我使用以下行将数据库中的项目添加到此列表:

public List<Conversation> getAllConversations() {
List<Conversation> conversationsList=new ArrayList<Conversation>();
String selectQuery = "SELECT * FROM " + TABLE_CONVERSATIONS+" order by id desc";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Conversation Conversation = new Conversation();
Conversation.setId(Integer.parseInt(cursor.getString(0)));
Conversation.setSender(cursor.getString(1));
Conversation.setTo(cursor.getString(2));
Conversation.setName(cursor.getString(3));
Conversation.setBio(cursor.getString(4));
Conversation.setPicture(cursor.getString(5));
Conversation.setTime(Integer.parseInt(cursor.getString(6)));
Conversation.setUnread(Integer.parseInt(cursor.getString(7)));
conversationsList.add(Conversation);
} while (cursor.moveToNext());
}
return conversationsList;
}

我想对特定项目使用setUnread方法,但如何操作?我知道我可以这样改变:

conversationsList.get(location).setUnread(1);

但我不知道位置。我需要使用另一个参数获取该项目。例如,我可以通过 sender 值获取该项目吗?

我需要这样的东西:

conversationsList.getByUsername("username").setUnread(1);

最佳答案

ArrayList 只能通过使用从零开始的索引来访问。如果您想使用另一个键(id 或用户名)访问元素,则需要使用 MapSparseArray(如果您使用数字键)。

由于您想通过“用户名”查找元素,因此我将在以下示例中使用 map :

public Map<String, Conversation> getAllConversations() {
final Map<String, Conversation> conversations = new HashMap<>();

Cursor cursor = ...;
while (cursor.moveToNext()) {
Conversation conversation = new Conversation();
...
conversations.put(conversation.getSender(), conversation);
}
cursor.close(); // don't forget to close your cursors!

return conversations;
}

然后你可以像这样查找对话:

conversations.get("John Doe").setUnread(1);

注意:您可以使用conversation.setTime(cursor.getInt(6));代替conversation.setTime(Integer.parseInt(cursor.getString) (6)));。 SQLite 数据库并不真正关心您存储的是字符串还是整数。

关于java - 更新数组列表中的特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27347373/

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