Search code examples
androidjsonparsinglistviewmessage

How to change the order of the messages from JSON?


I have such JSON response with messages from the server:

response: {
count: 74246,
items: [{
  id: 343194,
  body: 'Message 3',
  user_id: 123,
  from_id: 123,
  date: 1436513626,
  read_state: 1,
  out: 0
}, {
  id: 343190,
  body: 'Message 2',
  user_id: 123,
  from_id: 321,
  date: 1436513502,
  read_state: 1,
  out: 1
}, {
  id: 343187,
  body: 'Message 1',
  user_id: 123,
  from_id: 123,
  date: 1436513198,
  read_state: 1,
  out: 0
}]
}

I put it into the listview and I have such order:

  • Message 3
  • Message 2
  • Message 1

But I want to get the next order:

  • Message 1
  • Message 2
  • Message 3

So it means that the newest messages should be from the bottom to top. Probably I should build the listview from the bottom? But how? What should I do to achieve this? And sorry for my English :)


Solution

  • Set reverse order on ArrayList:

    ArrayList<Your_Model> mList = new ArrayList<Your_Model>();
    

    Set data in mList from JSON:

    Whenever you want to get List on reverse order, put below code.

    Collections.reverse(mList);
    

    Hope it will help you.