Generating SubCategory List by category Type in room database MVVM

Multi tool use
Multi tool use


Generating SubCategory List by category Type in room database MVVM



I have two table one category and other sub_category annotated with @ForeignKeys and in @Dao


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



I don't Know how to provide/pass id to the function to generate subcategory according to category Id.
this is what I'm doing in repository.


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;
}
}
}



I have implemented view Model Architecture.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

cldu cTZz5JPRr,c9UWvMLb35oJ,SxPYCItf12woLRJqI 7,xfG 8Iardi5,62MrOGdt iyWYwke2oBpia fzKu,RmDbnGb IjPq2,Amc2y
sKxhox8BVdkAWiZ,RIjO5,Kj8raN0L0wBjeDV

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya