rphello101
rphello101

Reputation: 1771

Display new items at the top of a ListView

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?

Upvotes: 10

Views: 23992

Answers (8)

sinay
sinay

Reputation: 31

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.

Upvotes: 3

Prodipta Mondal
Prodipta Mondal

Reputation: 11

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

Upvotes: 1

mes
mes

Reputation: 3621

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

}

Upvotes: 15

Pedro Trindade
Pedro Trindade

Reputation: 66

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).

Upvotes: 0

Swayam
Swayam

Reputation: 16354

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.

Upvotes: 3

krilovich
krilovich

Reputation: 3505

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

Upvotes: 0

aymeric
aymeric

Reputation: 3895

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

Upvotes: 11

kosa
kosa

Reputation: 66637

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

Upvotes: 20

Related Questions