This is my CartProductItemDao.
@Dao
public interface CartProductItemDao {
@Query("select * from CartProductItem where product__category__id = :id")
LiveData<List<CartProductItem>> getCartProductItemList(int id);
@Query("select * from CartProductItem where product__id in (:items)")
LiveData<List<CartProductItem>> getAllProductsSectionItem(ArrayList<Integer> items);
@Query("Select * from CartProductItem where product__fav_market_id =:id")
LiveData<List<CartProductItem>> geAllCartProductItemList(int id);
}
and this is my CartProductItemRepository.
public class CartProductItemRepository {
LiveData<List<CartProductItem>> cartProductItemList;
CartProductItemDao cartProductItemDao;
public CartProductItemRepository(Application application) {
TreaderOnlineDatabase treaderOnlineDatabase=TreaderOnlineDatabase.getInstance(application);
cartProductItemDao=treaderOnlineDatabase.cartProductItemDao();
}
public LiveData<List<CartProductItem>> getCartProductItemList(int cat_id) {
cartProductItemList= cartProductItemDao.getCartProductItemList(cat_id);
return cartProductItemList;
}
}
and this is my CartProductItemViewModel.
public class CartProductItemViewModel extends AndroidViewModel {
LiveData<List<CartProductItem>> cartProductItemList;
LiveData<List<CartProductItem>> productSectionItemList;
LiveData<List<CartProductItem>> allCartProductItem;
CartProductItemRepository cartProductItemRepository;
SharedPreferenceClass sharedPreferenceClass;
public CartProductItemViewModel(@NonNull Application application) {
super(application);
cartProductItemRepository=new CartProductItemRepository(application);
sharedPreferenceClass=new SharedPreferenceClass(getApplication());
//cartProductItemList=cartProductItemRepository.getCartProductItemList();
}
public LiveData<List<CartProductItem>> getCartProductItemList(int id) {
cartProductItemList=cartProductItemRepository.getCartProductItemList(id);
return cartProductItemList;
}
}
and I called getCartProductItemList(cat_is) from inside fragment and this is the code
@Override
public void recyclerViewOnClick2() {
CartProductItemViewModel cartProductItemViewModel;
cartProductItemViewModel=ViewModelProviders.of(this).get(CartProductItemViewModel.class);
int x=sharedPreferenceClass.getSharedPreferenceCatId();
cartProductItemViewModel.getCartProductItemList(x).observe(this, new Observer<List<CartProductItem>>() {
@Override
public void onChanged(List<CartProductItem> cartProductItems) {
product_categoriesFragmentAdpter.setArrayList(cartProductItems, getContext());
}
});
}
the problem when I make an update or delete for table or insert this observer called multiple times.
The lifecycle of the Fragment can be longer than the lifecycle of the View created. Replace this with viewLifecycleOwner for observing your liveData to avoid double subscriptions :
cartProductItemViewModel.getCartProductItemList(x).observe(viewLifecycleOwner, new Observer<List<CartProductItem>>() {
@Override
public void onChanged(List<CartProductItem> cartProductItems) {
product_categoriesFragmentAdpter.setArrayList(cartProductItems, getContext());
}
});
Make sure you don't observe liveData before onCreateView(), when using viewLifecycleOwner.
Source: https://developer.android.com/reference/androidx/fragment/app/Fragment.html#getViewLifecycleOwner()
https://www.youtube.com/watch?v=pErTyQpA390&feature=youtu.be
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With