6

greenDao を使用して関連テーブルのフィールドで並べ替える方法はありますか? たとえば、車のテーブルとドライバーのテーブルがあります。各車両には運転手が付きます。今、私は車 (例: 青) を照会し、ドライバーの名前で並べ替えたいと考えています。

4

4 に答える 4

5

QueryBuilder には、ソート順を指定するメソッドがあります。orderAsc(Property) など、"order..." で始まるメソッドを探します。

于 2013-04-18T13:45:10.840 に答える
2

Greendao ORM によって生成される (データ アクセス オブジェクト) DAO で QueryBuilder を使用できます。

使用前に定義する (アクティビティで)

 ProductDao productDao;
 DaoSession daoSession;

DaoMaster と DaoSession をアプリケーション スコープに配置する必要があります。Application を拡張するクラスの onCreate() 内。

DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(), "app-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
daoSession = new DaoMaster(db).newSession();

使う前に初期化

 daoSession = ((MyApplication) getApplication()).getDaoSession();
 productDao = daoSession.getProductDao();

このように結果を並べ替えて、アクティビティに表示できます。

private void refreshProducts() {
        switch (sorted_by){
            case SORT_BY_DATE:
                cardItems = productDao.queryBuilder().orderAsc(ProductDao.Properties.Id).list();
                setupRecyclerView();
                break;

            case SORT_BY_PRICE:
                cardItems = productDao.queryBuilder().orderDesc(ProductDao.Properties.Price).list();
                setupRecyclerView();
                break;

            case SORT_BY_POPULARITY:
                cardItems = productDao.queryBuilder().orderDesc(ProductDao.Properties.Name).list();
                setupRecyclerView();
                break;
        }
    }
于 2017-01-25T15:08:27.790 に答える