按类别生成子类别列表房间数据库MVVM中的类型

时间:2018-06-30 09:24:14

标签: java android viewmodel android-room

我有两个表一类,其他子类分别用@ForeignKeys和@Dao标注

@Query("SELECT * FROM TransSubCategory WHERE 
       TRANS_CATEGORY_ID LIKE :id ORDER BY transSubCategory ASC ")
LiveData<List<TransSubCategory>> getAlphabetizedTransSubCategoriesById(int id);

我不知道如何根据类别ID向函数提供ID或将ID传递给该函数以生成子类别。 这就是我在存储库中所做的。

public class TransSubCategoryRepository {
private static int id;

private TransSubCategoryDao mTransSubCategoryDao;
private LiveData<List<TransSubCategory>> mAllTransSubCategories;// change here
private LiveData<List<TransSubCategory>> mTransSubCategoriesByCategoryId;

public TransSubCategoryRepository(Application application) {
    ExpenseDatabase db = ExpenseDatabase.getDatabase(application);
    mTransSubCategoryDao = db.transSubCategoryDao();// update function in database.
    mAllTransSubCategories = mTransSubCategoryDao.getAlphabetizedTransSubCategories();// replace the function in Dao

    mTransSubCategoriesByCategoryId = mTransSubCategoryDao.getAlphabetizedTransSubCategoriesById(getId());
}
**Trying to provide id but not succeeded **
private int getId(int id){
    return id;
}
public LiveData<List<TransSubCategory>>getTransSubCategoriesById(){
    getId();
    return mTransSubCategoriesByCategoryId;
}

// Room executes all queries on a separate thread.
// Observed LiveData will notify the observer when the data has changed.
public LiveData<List<TransSubCategory>> getAllTransSubCategories() // replace the function in dao.
 {
    return mAllTransSubCategories;
}

// You must call this on a non-UI thread or your app will crash.
// Like this, Room ensures that you're not doing any long running operations on the main
// thread, blocking the UI.
public void insert (TransSubCategory transSubCategory) {
    new insertAsyncTask(mTransSubCategoryDao).execute(transSubCategory);
}

private static class insertAsyncTask extends AsyncTask<TransSubCategory, Void, Void> {

    private TransSubCategoryDao mTransSubCategoryAsyncTaskDao;

    insertAsyncTask(TransSubCategoryDao dao) {
         mTransSubCategoryAsyncTaskDao = dao;
    }

    @Override
    protected Void doInBackground(final TransSubCategory... params) {
         mTransSubCategoryAsyncTaskDao.insert(params[0]);
        return null;
    }
}
}

我已经实现了视图模型架构。

0 个答案:

没有答案