更新arraylist中的特定项目

时间:2014-12-07 20:21:39

标签: java android

我想更新arraylist中的特定项目。

这是Conversation类:

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);

1 个答案:

答案 0 :(得分:0)

只能使用从零开始的索引访问ArrayList。如果您想使用其他密钥(ID或用户名)访问元素,则需要使用MapSparseArray(如果您使用数字键)。

由于您想按“用户名”查找元素,我将在以下示例中使用地图:

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数据库并不关心您是存储字符串还是整数。