2014-12-07 17 views
0

我有一個列表和linkedhashmap.I正與在主類的頂部下面的行定義:我可以更新列表視圖,但如何?

List<Conversation> conversationsList=new ArrayList<Conversation>(); 
LinkedHashMap<String, Conversation> conversationsMap=new LinkedHashMap<String, Conversation>(); 

onCreate()我從分貝將數據傳輸到HashMap中然後轉移圖值以ArrayList和我使用此ArrayList在下面的行適配器視圖:

conversationsMap=_db.getAllConversations(); 
conversationsList=new ArrayList<Conversation>(conversationsMap.values()); 
conversationsAdapter=new ConversationsAdapter(this,conversationsList); 

但有件事我不understand.I可以更新這些行名單:

conversationsMap.get(room_name).increaseUnread(); 
((ConversationsAdapter) conversationsAdapter).updateConversations(conversationsList); 

它的工作,但我沒有更新conversationsList爲什麼這是工作我不明白?我剛剛更新了conversationsMap而不是conversationsList。任何人都可以向我解釋爲什麼這是工作?

在DB處理getAllConversations方法:

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

     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)));   
       conversations.put(cursor.getString(2),Conversation); 
      } while (cursor.moveToNext()); 
     } 
     cursor.close(); 
     return conversations; 
} 

這是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 increaseUnread() { 
     this.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; 
    } 
}   
+1

有沒有特別的原因您沒有使用[CursorAdapter](http://developer.android.com/reference/android/support/v4/widget/CursorAdapter.html)? – ianhanniballake 2014-12-08 00:01:32

回答

0

你熟悉你的適配器notifyDataSetChanged()notifyDataSetInvalidated()方法呢?線new ArrayList<Conversation>(conversationsMap.values());應該做你的地圖值的淺拷貝,檢查:數據集的每一個變化,這是用來在列表被吸入後/格,你應該後updateConversations(conversationsList);

也稱這兩種方法(可能是第一個)之一this stack thread about set copying欲瞭解更多信息

+0

你弄錯了。我的代碼正在工作,但爲什麼?我說我沒有更新conversationsList,但仍然是新增的unreadCount值。 – Okan 2014-12-08 00:08:54

+0

我必須承認我也被懇求。它看起來像對象從地圖得到,並放在列表是相同的對象,相同的參考。所以當你在一個集合中更新這個對象時,它也在另一個集合中更新。您可以通過記錄來檢查對象的ID:Log.i(「MyLog」,「object:」+ conversationsMap.get(room_name));'和等效的「getter」列表。他們將用十六進制id值打印,可能在你的情況下它將是相同的(因爲它是相同的對象/參考) – snachmsm 2014-12-08 00:18:30

相關問題