在我onCreateView
的{{1}}中,我尝试从数据库中获取数据。我使用Android房间。
如果我得到Fragment
或0行 - 我会显示按钮"更新"。用户按下此按钮并从服务器获取数据,然后插入到DB。之后我想要刷新null
并再次从DB加载数据。
我尝试在RecyclerView
中加载数据:
onCreateView
如果DB有数据 - 一切都很好。如果找不到数据,我会显示Button,然后按下make rest request并在此代码中获得结果
MyApplication.me().getDatabase().categoryDao().getAll()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(categories -> {
if (categories == null || categories.isEmpty()) {
price.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.INVISIBLE);
Toast.makeText(getActivity(), "Update", Toast.LENGTH_SHORT).show();
} else {
CategoryAdapter categoryAdapter = new CategoryAdapter(categories);
categoryAdapter.setOnItemClickListener(new ClickHandler());
recyclerView.setAdapter(categoryAdapter);
}
});
方法saveProducts在数据库中保存数据,之后我需要刷新适配器!!!但是!!!
@Override
public void onNext(ProductsResponse value) {
ProductService.me().saveProducts(value.getProductItems());
}
此新方法中的方法。我试过这个:
public void saveProducts(List<ProductItem> products) {
new Thread(() -> {
List<Product> productList = new ArrayList<>();
for (ProductItem item : products) {
............
productList.add(product);
}
MyApplication.me().getDatabase().productDao().insertAll(productList);
}).start();
}
并在每次插入后触发此代码
@Query("SELECT * FROM categories")
Flowable<List<Category>> getAll();
但适配器无法刷新
答案 0 :(得分:0)
您需要再次显示您的RecyclerView:
.subscribe(categories -> {
if (categories == null || categories.isEmpty()) {
price.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.INVISIBLE);
Toast.makeText(getActivity(), "Update", Toast.LENGTH_SHORT).show();
} else {
price.setVisibility(View.INVISIBLE); // HIDE BUTTON
recyclerView.setVisibility(View.VISIBLE); //SHOW LIST
CategoryAdapter categoryAdapter = new CategoryAdapter(categories);
categoryAdapter.setOnItemClickListener(new ClickHandler());
recyclerView.setAdapter(categoryAdapter);
}
});