ListView with max number of items showing last items

I'm developing an application with a chat and I want to show only the last 50 messages by default. I'm using a ListView with a custom ListAdapter and I have accomplished limit the number of messages doing this:

    @Override
    public int getCount() {
        return Math.min(mMessages.size(), mNumberMessagesToDisplay);
    }

My problem is that now the list shows the FIRST 50 messages instead of the last. I have not idea of how solve it. Can anyone help me?

Thanks!!!!!

Upvotes: 0

Views: 75

Answers (1)

Fabio Venturi Pastor
Fabio Venturi Pastor

Reputation: 2529

Just create an ArrayList of your messages and add the last message to top of the listView and delete the last message of the bottom:

listView.addHeaderView(yourView);
arrayListOfMesagges.add(message);

    if (arrayListOfMesagges.length>=50){
      listView.removeViewAt(arrayListOfMesagges.length);
      arrayListOfMesagges.remove(arrayListOfMesagges.length);
}

Thats all

Upvotes: 1

Related Questions