0

I googled a lot but didn't find any answer to my problem:

I got a Cursor that queries a table that contains a latitude and a longitude. I want to order a ListView by the distance to the user. The second answer in here showed me how to order by distance.

But now let's say the user moves and the second item is now more near than the first one. How can I reorder my list now?

Not sure if i could provide any code that would be helpful, because it's more or less a generic question.

thanks alot

Community
  • 1
  • 1
zinninger
  • 1
  • 1
  • every time the user moves you need to requery and the the calculations all over again – tyczj Nov 25 '14 at 17:20
  • `The second answer in this thread` ... hehehe ... order of the answers is changing dynamically – Selvin Nov 25 '14 at 17:25
  • @Selvin but the order by is a String that is definded once in the onCreateView method of my fragment. this means it will always be ordered by the starting point. – zinninger Nov 25 '14 at 17:28
  • @tyczj how would I do that? – zinninger Nov 25 '14 at 17:28
  • what do you mean how do you do that, you know how to do it the first time so just do the same thing again – tyczj Nov 25 '14 at 17:30
  • When you calculated the distance first time using the current location(in lattitude and longitude), then when the user moves to another location get the new location of user and recalculate latitude and longitude. – Mohammed Ali Nov 25 '14 at 17:36
  • so you mean that i should call changeCursor? i don't think that this is a good idea – zinninger Nov 25 '14 at 17:36
  • @MohammedAli yes, obviously. But the question is: How do I reorder it? How do I notify the Cursor or Adapter that the" order by" value has changed? – zinninger Nov 25 '14 at 17:41
  • to notify adapter of changes see these: http://stackoverflow.com/q/3669325/3879470 http://stackoverflow.com/q/14503006/3879470 – Mohammed Ali Nov 25 '14 at 18:14

1 Answers1

0

If the ordering is defined as a calculation in SQL, then I think you'll have to requery. (The cursor will not dynamically recalculate the "ORDER BY" clause, this happens at query time in SQL).

Alternatively, you could change your adapter to one backed by a list instead of a cursor, and implement a Comparator to sort the data. Then notify the adapter that the data has changed. Assuming each row in the list is backed by an object of type Item:

Collections.sort(listOfItems,new Comparator<Item>(){
public int compare(Item i1, Item i2){
    /* Compare your items here. Return
     * -1 if i1 < i2
     *  0 if i1 = i2
     *  1 if i1 > i2
     */
}});
JstnPwll
  • 8,585
  • 2
  • 33
  • 56
  • as i mentioned, i am working with a Cursor, so I am using a CursorAdapter. That's why i cant use a comparator – zinninger Nov 25 '14 at 17:40