I have a list and linkedhashmap.I am defining with following lines in top of main class:
List<Conversation> conversationsList=new ArrayList<Conversation>();
LinkedHashMap<String, Conversation> conversationsMap=new LinkedHashMap<String, Conversation>();
In onCreate()
I am transferring data from db to hashmap then transferring map values to arraylist and I am using this arraylist in adapterview with following lines:
conversationsMap=_db.getAllConversations();
conversationsList=new ArrayList<Conversation>(conversationsMap.values());
conversationsAdapter=new ConversationsAdapter(this,conversationsList);
But there is something I don't understand.I can update the list with these lines:
conversationsMap.get(room_name).increaseUnread();
((ConversationsAdapter) conversationsAdapter).updateConversations(conversationsList);
It's working but I didn't update conversationsList
why this is working I don't understand ? I just updated conversationsMap
not the conversationsList
. Can anyone explain to me why this is working ?
In db handler getAllConversations
method:
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;
}
And this is the Conversation
class:
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;
}
}