2014-12-07 74 views
1

我想更新arraylist中的特定项目。更新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); 

但我不知道location.I需要与另一parameter.e.g拿到项目,我可以通过sender值获得该项目?

我需要的是这样的:

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

回答

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

+0

我在适配器中使用此列表。如何继续使用? – Okan 2014-12-07 21:22:28

+0

@Okan您可以通过Map.values()函数将地图转换为列表 – Dmitry 2014-12-07 21:24:58

+0

我需要一个有序列表hashmap并不保证项目位置。我需要将新项目添加到此hashmap的顶部。如何才能做到这一点? – Okan 2014-12-07 21:54:34