Display new items at the top of a ListView
Asked Answered
H

8

10

I'm using a list to populate a ListView (). The user is able to add items to the list. However, I need the items to be displayed at the top of the ListView. How do I insert an item at the beginning of my list in order to display it in reverse order?

Handmaid answered 31/8, 2012 at 19:16 Comment(1)
What is the source of the data? If you're reading it from a database, you could always change the order the query returns with order by or equivalent.Pate
F
11

You should probably use an ArrayAdapter and use the insert(T, int) method.

Ex:

ListView lv = new ListView(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.id...);
lv.setAdapter(adapter);
...
adapter.insert("Hello", 0);
Filamentous answered 31/8, 2012 at 19:40 Comment(1)
I just scrapped my list and souly used the adapter. Worked great! Thanks.Handmaid
H
20

By default list adds elements at bottom. That is why all new elements you add will show at bottom. If you want it in reverse order, may be before setting to listadapter/view reverse the list

Something like:

Collections.reverse(yourList);
Holster answered 31/8, 2012 at 19:19 Comment(4)
Good idea, but I kept adding items to the list and kept reversing it, I'd essentially be adding, in an alternating fashion, an item to each end of the list. Since I need to save the data with SharedPreferences which only saves basic data types, it would be quite a hassle to keep track of two lists.Handmaid
Is good, but not work when you try with objects custom + tags !Bacteria
@delive: When object is custom, you need to override equals() and hashcode() method in your custom class users.csc.calpoly.edu/~kmammen/documents/java/….Holster
I know but change background @override click listview (in my case ExpandableListView) for the positions but my data from server and not work correctly I try all form, shenhengbin.wordpress.com/2012/03/25/… with this example dinamically you can't :), you can try :)Bacteria
L
15

Another solution without modifying the original list, override getItem() method in the Adapter

@Override
public Item getItem(int position) {
    return super.getItem(getCount() - position - 1);
}

Updated: Example

public class ChatAdapter extends ArrayAdapter<ChatItem> {
public ChatAdapter(Context context, List<ChatItem> chats) {
    super(context, R.layout.row_chat, chats);
}

@Override
public Item getItem(int position) {
    return super.getItem(getCount() - position - 1);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = inflater.inflate(R.layout.row_chat, parent, false);
    }

    ChatItem chatItem = getItem(position);
    //Other code here

    return convertView;
}

}

Love answered 13/6, 2014 at 11:59 Comment(4)
This is a very elegant solution. I made a minor change so I could use it in anonymous ArrayAdapters that don't expose the data items: return super.getItem(getCount() - 1 - position);Diaz
This works but when scrolling the first item in the list seems like a duplicate to the last item list. I think this problem has to do with the recycling of the listview items (not sure).Vonvona
Could you please tell how to override getItem() method in adapter?Denny
not work onlick change color, because always select inverted return super.getItem(getCount() - position - 1);Bacteria
F
11

You should probably use an ArrayAdapter and use the insert(T, int) method.

Ex:

ListView lv = new ListView(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.id...);
lv.setAdapter(adapter);
...
adapter.insert("Hello", 0);
Filamentous answered 31/8, 2012 at 19:40 Comment(1)
I just scrapped my list and souly used the adapter. Worked great! Thanks.Handmaid
C
3

The ListView displays the data as it is stored in your data source.

When you are adding in your database, it must be adding the elements in the end. So, when you are getting all the data via the Cursor object and assigning it to the ArrayAdapter, it is in that order only. You should basically be trying to put data in the beginning of the database, rather that in the end, by having some time-stamp maybe.

Using ArrayList, you can do it by Collections.reverse(arrayList) or if you are using SQLite, you can use order by.

Clabo answered 31/8, 2012 at 19:20 Comment(2)
No I think this is wrong. For listview you need to pass array/list as input. In database unless you have timestamp of insertion, you can't do anything.Holster
@Nambari : I am sorry. What I actually meant was that to retrieve the data from the database by using a Cursor object and then put it in an ArrayAdapter. Something like this #5718232. Will update my answer. Thanks for your comment.Clabo
D
3

You can add element at the beginning of the list: like

arraylist.add(0, object)

then it will always display the new element at the top.

Danutadanya answered 4/12, 2017 at 5:38 Comment(1)
Perfect Answer :)Farfamed
D
1

mBlogList is a recycler view...

mBlogList=(RecyclerView) findViewById(R.id.your xml file);
mBlogList.setHasFixedSize(true);


LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mBlogList.setLayoutManager(mLayoutManager);//VERTICAL FORMAT
Dwyer answered 29/9, 2017 at 17:22 Comment(0)
C
0

You could always have a datestamp in your objects and sort your listview based on that..

   public class CustomComparator implements Comparator<YourObjectName> {
        public int compare(YourObjectName o1, YourObjectName o2) {
            return o1.getDate() > o2.getDate() // something like that.. google how to do a compare method on two dates
        }
    }

now sort your list

Collections.sort(YourList, new CustomComparator()); 

This should sort your list such that the newest item will go on top

Context answered 31/8, 2012 at 19:50 Comment(0)
T
0

You can always use a LinkedList instead and then use addFirst() method to add elements to your list and it will have the desired behaviour (new items at the top of the ListView).

Truncation answered 29/4, 2013 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.