创建查询以返回按当前位置的距离排序的CursorLoader

时间:2016-12-22 14:07:32

标签: android sqlite sorting android-cursorloader

美好的一天, 我一直在寻找和寻找类似的东西,但到目前为止我还没有找到与我的问题有关的任何内容。

我有一个sqlite数据库,其地理位置存储了一堆地方,例如:

"CREATE TABLE " + WifiEntry.TABLE_NAME + " ( "
            + WifiEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + WifiEntry.COLUMN_NAME + " TEXT NOT NULL, "
            + WifiEntry.COLUMN_LONGITUDE + " REAL, "
            + WifiEntry.COLUMN_LATITUDE + " REAL, "
            + WifiEntry.COLUMN_OPINION + " REAL DEFAULT 0 );";

然后我有一个内容提供商来处理CRUD操作和加载程序来访问它。 我还有一个名为GeoPoint的类来计算两点之间的距离

public class GeoPoint {
private double lng, lat;
...

   public GeoPoint(double lng, double lat)
   {
      this.lng= lng;
      this.lat= lat;
   }

   public double distance(GeoPoint p)
   {
      returns the distance between this geopoint and p...
   }

正如您所看到的,我不会在数据库中找到与currentLocation存储距离的列,以及因此而定期更改的位置。我可以很容易地计算出我所在的位置和地点的距离,但是如何按距离排序以在ListView中显示?

在onCreateLoader中我有这个,正如你所看到的,我按名称排序很好,但是,如何按当前距离排序,如何将其放在投影中?

String [] projection = new String[]
                    {
                            WifiEntry._ID,
                            WifiEntry.COLUMN_NAME,
                            WifiEntry.COLUMN_LATITUDE,
                            WifiEntry.COLUMN_LONGITUDE,
                            WifiEntry.COLUMN_OPINION
                    };
            SharedPreferences prefs =
                    PreferenceManager.getDefaultSharedPreferences(getActivity());
            int sortPref = Integer.parseInt(prefs.getString(
                    getString(R.string.settings_sort_criteria_key), "0"));
            switch (sortPref)
            {
                case 0:
                    return new CursorLoader(
                            getActivity(),          // Parent activity context
                            WifiEntry.CONTENT_URI,  // Provider content URI to query
                            projection,             // Columns to include in the resulting Cursor
                            null,                   // No selection clause
                            null,                   // No selection arguments
                            WifiEntry.COLUMN_WIFI_NAME + " ASC");
                case 1: 
                // this is the **bydistance CASE**
                String sel =
                        WifiEntry.COLUMN_LONGITUDE + " " +
                                currentPosition.getLongitud()

                    WifiEntry.COLUMN_LATITUDE + " " +
                            currentPosition.getLatitud()
                           ??? 
                    //COLUMN1 + " * " + COLUMN2 + " as data",

谢谢!

0 个答案:

没有答案