1

I want to update specific item in arraylist.

This is 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 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;
    }
}

I am adding items from database to this list with following lines:

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

I want to use setUnread method for specific item but how ? I know I can change like this:

conversationsList.get(location).setUnread(1);

But I don't know the location.I need to get the item with another parameter.e.g can I get the item by sender value ?

I need something like this:

conversationsList.getByUsername("username").setUnread(1);
Okan
  • 1,379
  • 3
  • 25
  • 38

1 Answers1

0

An ArrayList can only be accessed by using a zero-based index. If you want to acces the elements by using another key (id or username) you need to use a Map or SparseArray (if you use a numeric key).

Since you wanted to lookup elements by "username", I'll use a map in the following example:

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

Then you can look up conversations like this:

conversations.get("John Doe").setUnread(1);

Note: You can use conversation.setTime(cursor.getInt(6)); instead of conversation.setTime(Integer.parseInt(cursor.getString(6)));. The SQLite database does not really care whether you store a string or an integer.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • I was using this list in an adapter.How can continue to use ? – Okan Dec 07 '14 at 21:22
  • @Okan you can convert Map to List via Map.values() function – Dmitry Dec 07 '14 at 21:24
  • I need an ordered list hashmap doesn't guarantees of item positions.And I need to add new items to top of this hashmap.How can I do this ? – Okan Dec 07 '14 at 21:54
  • @Okan `TreeMap` guarantees ordering. You can even sort it by time if you implement a comparator or make the `Conversation` comparable. – Eugen Pechanec Dec 07 '14 at 23:44
  • I have one more question.With this line : conversations.get("John Doe").setUnread(1); we are not setting the list we are just updating the hashmap right ? Can you look at this I asked a question about this:http://stackoverflow.com/questions/27349365/i-can-update-the-listview-but-how – Okan Dec 08 '14 at 00:14
  • What happens: 1) You find the element with key "John Doe". 2) You call `setUnread` on that element. If you put this element to a map and a list at the same time both will change - they are the same instance in memory. This does not apply to your other post - first you make the values and then you make copies of the values not the references. – Eugen Pechanec Dec 08 '14 at 00:32
  • So I can trust this right ? I am really surprised :) Thank you – Okan Dec 08 '14 at 11:07
  • "If your mother tells you she loves you... Verify." If you're not sure, test it. – Eugen Pechanec Dec 08 '14 at 11:10