0

I'm using a room database for saving some apps details in a table. And getting live data.

But I'm unable to use conditions.

@Query("SELECT * FROM apps WHERE parent LIKE: parent")
    LiveData<List<App>> getAllApp(String parent);

Here is the condition I'm trying to use in DAO. But I can't figure, where should I pass the parameter in REPOSITORY and VIEWNODEL class;

Please help me. May be my question is not that clear but please help me.

kish0n
  • 127
  • 3
  • 10

1 Answers1

0
public class AppRepository {

    private AppDao appDao;
    private LiveData<List<App>> appLiveData;


    AppRepository(Application application){
        Database database = Database.getDatabase(application);
        appDao = database.appDao();
        appLiveData = appDao.getAllApp();
    }


    LiveData<List<App>> getAllApp(){
        return appLiveData;
    }

    void insert(App app){
        Database.writer.execute(() ->{
            appDao.insert(app);
        });
    }

    void delete(App app){
        Database.writer.execute(() ->{
            appDao.delete(app);
        });
    }
}
public class AppViewModel extends AndroidViewModel {

    private AppRepository repository;
    private LiveData<List<App>> appLiveData;

    public AppViewModel(@NonNull Application application) {
        super(application);

        repository = new AppRepository(application);
        appLiveData = repository.getAllApp();
    }

    public LiveData<List<App>> getAllApp( ){
        return appLiveData;
    }

    public void insert(App app){
        repository.insert(app);
    }

    private void delete(App app){
        repository.delete(app);
    }
}
pppery
  • 3,731
  • 22
  • 33
  • 46
kish0n
  • 127
  • 3
  • 10
  • This would be a better answer if you explained how the code you provided answers the question. – pppery Jul 10 '20 at 03:15