0

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;
    }
}           
Okan
  • 1,379
  • 3
  • 25
  • 38
  • 1
    Is there a particular reason you aren't using a [CursorAdapter](http://developer.android.com/reference/android/support/v4/widget/CursorAdapter.html)? – ianhanniballake Dec 08 '14 at 00:01

1 Answers1

0

are you familiar with notifyDataSetChanged() and notifyDataSetInvalidated() methods in your adapter? after every change of data set which is used to be drawn in List/Grid you should call one of these two methods (probably first one) after updateConversations(conversationsList);

also: line new ArrayList<Conversation>(conversationsMap.values()); should do a shallow copy of your map values, check this stack thread about set copying for more info

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • You got it wrong.My code is working.But why ? I said I didn't update conversationsList but nevertheless it's uptading listview with new unreadCount value. – Okan Dec 08 '14 at 00:08
  • I must admit that I'm also supprised. It's looks like Object get from map and put to List is the same Object, same reference. so when you updating this Object in one set it also updates in another. you might check ids of objects by logging them: `Log.i("MyLog","object: "+conversationsMap.get(room_name));` and equivalent "getter" for list. they will print with hex id value and probably in your case it will be the same (as it's same object/reference) – snachmsm Dec 08 '14 at 00:18