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

标签 java android

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

这是对话类:

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/

相关文章:

java - 将 Web Controller 添加到 Akka Actor 系统

javascript - Firebase Twitter 身份验证和 PhoneGap

android - 将库导入 Android Studio 项目

android - 自动更新 Android 应用程序

java - 从 SQLite 获取数据并显示到 RecyclerView 中

java - 有没有一种方法可以使用嵌入式数据库来测试 Postgres Jsonb 查询以进行单元测试?

java - 无法使用 Jackson 从 JSON 获取单个值

javascript - 从两个动态下拉列表中选择值

android - 保留选中的项目 ListView 过滤器

javascript - ngCamera 不适用于 iOS