Answer the question
In order to leave comments, you need to log in
Why is the data from the Room not being loaded into the RecyclerView on request?
I have Enity and DAO
@Entity(tableName = GarmentTable.NAME,
foreignKeys = @ForeignKey(entity = GarmentType.class, parentColumns = GarmentTypeTable.Cols.ID, childColumns = GarmentTable.Cols.TYPE))
public class Garment {
@PrimaryKey( autoGenerate = true)
@ColumnInfo(name = GarmentTable.Cols.ID)
private long mId;
@ColumnInfo(name = GarmentTable.Cols.TITLE)
private String mTitle;
@ColumnInfo(name = GarmentTable.Cols.TYPE, index = true)
private int mTypeId;
//getters setters
}
@Dao
public interface GarmentDao {
@Query("SELECT * FROM " + GarmentTable.NAME)
LiveData> getAll();
@Query("SELECT * FROM " + GarmentTable.NAME + " WHERE :clause")
LiveData> getByClause(String clause);
}
In the repository, I have two methods to get all Garments or garments for a specific request
public class GarmentRepository {
private GarmentDao mGarmentDao;
private LiveData> mGarments;
GarmentRepository(Application application) {
MyRoomDatabase db = MyRoomDatabase.getDatabase(application);
mGarmentDao = db.garmentDao();
mGarments = mGarmentDao.getAll();
}
public LiveData> getAllGarments() {
return mGarments;
}
public LiveData>
return mGarmentDao.getByClause(whereClause);
}
}
In the ViewModel I make requests to the repository
public class GarmentViewModel extends AndroidViewModel {
private GarmentRepository mRepository;
private LiveData> mGarments;
public GarmentViewModel(Application application) {
super(application);
mRepository = new GarmentRepository(application);
mGarments = mRepository.getAllGarments();
}
public LiveData> getGarments() {
return mGarments;
}
public void setGarments(String whereClause) {
mGarments = mRepository.getGarments(whereClause);
}
}
In RecyclerView I add garments
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
final GarmentListAdapter adapter = new GarmentListAdapter(getActivity());
recyclerView.setAdapter(adapter);
mGarmentViewModel = ViewModelProviders.of(this).get(GarmentViewModel.class);
if(whereClause != null) {
mGarmentViewModel.setGarments(whereClause);
}
mGarmentViewModel.getGarments().observe(this, new Observer>() {
@Override
public void onChanged(@Nullable final List garments) {
adapter.setGarments(garments);
}
});
The question is that if I make a request for all garments, then they are displayed for me, if I enter some query (for example, whereClause = GarmentTable.Cols.TYPE + " = " + (i + 1); ), then I have an empty list is displayed. The program itself is much larger, I took from it separate parts that relate specifically to this problem. Are there any suggestions why this is so?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question