Dynamic Sort CursorAdapter

I am currently using CursorAdapter with Loader . The data downloaded by Loader contains the location. I want to order my goods from the nearest to the farthest. I am already calculating the distance to display it, now I need to find a way to dynamically sort the data.

I could probably change the CursorLoader every time the data is changed, but repeating the same SQL query again and again, just to reorder, seems somehow redundant to me ...

Is there a way to dynamically sort data in a CursorAdapter ?

+7
android sorting android-location android-listview android-cursoradapter
source share
1 answer

You must use sortOrder - the last parameter to the CursorLoader Contractor .

Suppose your table has a column named lat for latitude and lon for longitude. The user's location is also described through the latitude / longitude of userLat and userLon . For sorting, we do not need to know the exact distance - an approximate distance will be enough. For this you can use the Pythagorean theorem.

sortOder will look like this: abs(lat - userLat)* abs(lat - userLat) + abs(lon-userLon)*abs(lon-userLon) . This calculation contains only support for Sqlite statements.

Note 1: you must modify the code above to provide the correct line where userLat and userLon are replaced with their values.

Note 2: you cannot use this formula to calculate the exact distance.

+4
source share

All Articles