2

I am working on a XMPP based chat in android.. and I am struck at a point where I need to update the position of an item in the listview to the top in case a new.message arrives.

The use case is.. I am on Contacts screen of the app and a new message comes.. so this contact should move to top of the list and get bold. This is what is similar to whatsapp as well

How can this be done. My class imolemebts activity and i have implemented custom list adapter.

So howcan I find if an item exists in the listview and secondly how to dynamically change position

Akash Khanna
  • 159
  • 3
  • 12

2 Answers2

2

First, keep in mind that a ListView is just a representation of a list of Objects. So if you want to know if an item is in the ListView, you just have to check if the corresponding Object is in your list of Objects.

Is the same when you want to change the position of one item, you have to change the position of the Object in the list.

Start by defining these objects:

private ArrayList<MyObject> lists = new ArrayList<MyObject>();
private MyCustomAdapter myAdapter;

The first time you create your ListView, just do as usually:

//fill your list with your objects
lists.add(myObject1);
lists.add(myObject2);
lists.add(myObject3);
//create and set the adapter
myAdapter = new MyCustomAdapter(..., ..., lists);
myListView.setAdapter(myAdapter);

Now you can know if your lists contains a specific object (which is the same that checking if an item is in your ListView) by simply testing that:

lists.contains(anObject);

Then, if you want to change the position of a specific item in the ListView, you have to create a new list and put the elements in the correct order. You can use something like that (not tested but it should work):

private ArrayList<MyObject> moveItemToTop(ArrayList<MyObject> lists, int positionOfItem) {
    if (lists == null || positionOfItem < 0 || positionOfItem >= lists.size()) {
        return lists;
    }

    ArrayList<MyObject> sortedList = new ArrayList<MyObject>();
    //add the item to the top
    sortedList.add(lists.get(positionOfItem));

    for (int i=0; i<lists.size(); i++) {
        if (i != positionOfItem) {
            sortedList.add(lists.get(i));
        }
    } 

    return sortedList;
}

Or even this (which is way easier...).

Finally, call these two methods to update your ListView:

myAdapter = new MyCustomAdapter(..., ..., moveItemToTop(lists, itemPosition));
myListView.setAdapter(myAdapter);
Community
  • 1
  • 1
G.T.
  • 1,557
  • 1
  • 12
  • 24
0

This is how I resolved it

private void moveMessageToTop(MessageObject message) {
        int index = 0;
        for (Friends friend : mFriends) {
            if (friend.getName().equalsIgnoreCase(message.getFrom().split("@")[0])) {
                index = mFriends.indexOf(friend);
                break;
            }
        }

        if (index != 0) {
            mFriends.add(0,new Friends(message.getFrom().split("@")[0], message
                    .getMessage()));
        } else {
            Friends frnd = mFriends.get(index);
            frnd.setStatus(message.getMessage());
            mFriends.add(0, frnd);
            mFriends.remove(index);
        }

        ((ListAdapter) lvFriends.getAdapter()).notifyDataSetChanged();
    }
Akash Khanna
  • 159
  • 3
  • 12