0

My database has several records and each record has object's location (2 fields: latitude and longitude). I need to calculate the distance from the current user's location to each object (with distanceBetween method). Then this value should be written into another database field (distance).

How should I do that?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
LA_
  • 19,823
  • 58
  • 172
  • 308

1 Answers1

1

You shouldn't... calculated fields should not be stored. You should calculate things as you need them.

ekawas
  • 6,594
  • 3
  • 27
  • 39
  • I need to display the result field (distance) in the ListView. How can I do that without database update? – LA_ May 24 '11 at 17:23
  • 1
    Create an ArrayAdapter on the fly? You can get your lat and long values from the database as a cursor, and then loop through then using distanceBetween(), every time you calculate the value, put it into the array. Then put that array into the ArrayAdapter constructor. By the way that might be a horrible way of doing it as you are making the way Cursor works a bit redundant but it works. – Infiniti Fizz May 24 '11 at 17:40
  • I agree with infiniti fizz. You need to calculate them as you need them. One way would be to do what they said. You could also look into using a `SimpleCursorAdapter` and then implementing `SimpleCursorAdapter.ViewBinder`. In the view binder, `setViewValue()`, you could apply your `distanceBetween()` function. – ekawas May 24 '11 at 21:16
  • examples (what they do is different, but the idea is the same): 1. http://stackoverflow.com/questions/1505751/android-binding-data-from-a-database-to-a-checkbox-in-a-listview 2. http://stackoverflow.com/questions/5573539/android-using-simplecursoradapter-to-set-colour-not-just-strings 3. http://www.anddev.org/view-layout-resource-problems-f27/displaying-a-formatted-date-from-db-t10765.html – ekawas May 24 '11 at 21:20