Skip to content

Instantly share code, notes, and snippets.

@ayetolusamuel
Last active November 25, 2019 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayetolusamuel/9bc0a23b23e5f6436e3d0dc347e47fee to your computer and use it in GitHub Desktop.
Save ayetolusamuel/9bc0a23b23e5f6436e3d0dc347e47fee to your computer and use it in GitHub Desktop.
Am trying to fetch all data from sqlite database based on a particular condition i.e i have a particular column name price that contact the product price
in format(3,000.00).
so my concern now is to perform a condition such as if price is less than 10,000, my query should return data base on the query..
my implementation
Am using mvvm architecture.
this is my dao implementation(method to fetch data)
@Query("SELECT * FROM products WHERE price <= :price")
LiveData<List<Product>> getProductByPrice(String price);
Repo:
public LiveData<List<Product>> getProductByPrice(String price){
return productDao.getProductByPrice(price);
}
viewModel:
public LiveData<List<Product>> getProductBasePrice(String price){
return mRepository.getProductByPrice(price);
}
this my implementation(when calling this function i pass 15,000 as an argument)
private void productBasePrice(String price) {
viewModel.getProductBasePrice(price)
.observe(getActivity(), new Observer<List<Product>>() {
@Override
public void onChanged(@Nullable List<Product> products) {
Collections.shuffle(products); //to shuffle the product list
Product product1 = products.get(0);
Product product2 = products.get(1);
Product product3 = products.get(2);
Product product4 = products.get(3);
ArrayList<Product> arrayList = new ArrayList<>();
arrayList.add(product1);
arrayList.add(product2);
arrayList.add(product3);
arrayList.add(product4);
productAdapter.setProduct(arrayList);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
recyclerView.setAdapter(productAdapter);
RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecorator(ContextCompat.getDrawable(getContext(), R.drawable.divider));
recyclerView.addItemDecoration(dividerItemDecoration);
}
});
}
this is the link to database image: https://drive.google.com/open?id=1OOl_I2T8L72qMSXU3smiuWeqeyh315bX
this is the link to the logcat: https://drive.google.com/open?id=1XB3ZNx94WxPL05DFlMaYCQTJHMZHJfyE
what i want to achieve is to retrieve all product details if the price is less than or equal to 15,000...
Sir, please help with efficient way to achieved this sir.
i have try many methods in achieving this but not positive result sir.
Thanks..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment