具有房间数据库的Android分页;RecyclerView:未连接适配器;跳过布局
我正在使用Android寻呼和房间数据库。我将使用改装来获取数据。但我得到的错误是没有连接适配器;跳过布局。我搜索了很多,但没有找到解决方案。基本url正在工作,出于安全原因,我只是隐藏基本url具有房间数据库的Android分页;RecyclerView:未连接适配器;跳过布局,android,android-recyclerview,pagination,Android,Android Recyclerview,Pagination,我正在使用Android寻呼和房间数据库。我将使用改装来获取数据。但我得到的错误是没有连接适配器;跳过布局。我搜索了很多,但没有找到解决方案。基本url正在工作,出于安全原因,我只是隐藏基本url private StoreAdapter storeAdapter; private Store_ViewModel store_viewModel; private RecyclerView recyclerView; private static final Str
private StoreAdapter storeAdapter;
private Store_ViewModel store_viewModel;
private RecyclerView recyclerView;
private static final String URL_DATA="https://xxxx/";
//insertion
private Store_Repository store_repository;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
store_repository=new Store_Repository(getApplication());
//adapter
storeAdapter=new StoreAdapter(getApplicationContext(), this);
//recycler
recyclerView=findViewById(R.id.recycler_store);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
store_viewModel=new ViewModelProvider(this).get(Store_ViewModel.class);
store_viewModel.pagedListLiveData.observe(this, new Observer<PagedList<StoreModel>>() {
@Override
public void onChanged(PagedList<StoreModel> storeModels) {
storeAdapter.submitList(storeModels);
recyclerView.setAdapter(storeAdapter);
}
});
getAllProducts();
}
private void getAllProducts() {
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(URL_DATA)
.addConverterFactory(GsonConverterFactory.create())
.build();
//calling api
Api api=retrofit.create(Api.class);
Call<List<StoreModel>>call=api.getAllProducts();
call.enqueue(new Callback<List<StoreModel>>() {
@Override
public void onResponse(Call<List<StoreModel>> call, Response<List<StoreModel>> response) {
if (response.isSuccessful())
{
store_repository.insert(response.body());
}
}
@Override
public void onFailure(Call<List<StoreModel>> call, Throwable t) {
Toast.makeText(MainActivity.this, "Something get Wrong", Toast.LENGTH_SHORT).show();
}
});
}
ApiInterface
public interface Api {
@GET("/get-products")
Call<List<StoreModel>>getAllProducts();
}
下面是Dao类
@Dao
public interface StoreDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(List<StoreModel> storeModels);
@Query("DELETE FROM store")
void deleteAll();
@Query("SELECT * FROM store ORDER BY database_id ASC")
DataSource.Factory<Integer,StoreModel>getAllItems();
}
@Dao
公共接口StoreDao{
@插入(onConflict=OnConflictStrategy.REPLACE)
无效插入(列出存储模型);
@查询(“从存储中删除”)
void deleteAll();
@查询(“按数据库_id ASC从门店订单中选择*)
DataSource.FactorygetAllItems();
}
SetrecyclerView.setAdapter(storeAdapter)代码>在观察者之外,因为每次观察数据时,它都会设置适配器。因此,adapter
仅连接到recyclerView
一次。像
recyclerView.setAdapter(storeAdapter);
store_viewModel.pagedListLiveData.observe(this, new Observer<PagedList<StoreModel>>() {
@Override
public void onChanged(PagedList<StoreModel> storeModels) {
storeAdapter.submitList(storeModels);
//recyclerView.setAdapter(storeAdapter);
}
});
然后,您的界面应如您所期望的那样如下所示
ResponseObject
此处
public interface Api {
@GET("/get-products")
Call<ResponseObject> getAllProducts();
}
公共接口Api{
@获取(“/获取产品”)
调用getAllProducts();
}
通过在observeronCreateViewHolder()外部设置recyclerView调用失败时,返回null,为什么要这样做?我忘了提到-返回新的StoreViewHolder(LayoutFlater.from(parent.getContext())。充气(R.layout.store\u card,parent,false));但是仍然没有得到数据你从API得到的响应数据正确吗?@是的-我也在邮递员那里查过了
@Entity(tableName = "store",indices = @Index(value="product_id",unique = true))
public class StoreModel {
@PrimaryKey(autoGenerate = true)
private int database_id;
@SerializedName("product_id")
private int product_id;
@SerializedName("product_brand_id")
private int product_brand_id;
@SerializedName("product_name")
private String product_name;
@SerializedName("product_code")
private int product_code;
@SerializedName("mrp")
private int mrp;
@SerializedName("price")
private int price;
@SerializedName("product_weight")
private int product_weight;
@SerializedName("product_weight_unit")
private String product_weight_unit;
public StoreModel() {
}
public StoreModel(int product_id, int product_brand_id, String product_name, int product_code, int mrp, int price, int product_weight, String product_weight_unit) {
this.product_id = product_id;
this.product_brand_id = product_brand_id;
this.product_name = product_name;
this.product_code = product_code;
this.mrp = mrp;
this.price = price;
this.product_weight = product_weight;
this.product_weight_unit = product_weight_unit;
}
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public int getProduct_brand_id() {
return product_brand_id;
}
public void setProduct_brand_id(int product_brand_id) {
this.product_brand_id = product_brand_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public int getProduct_code() {
return product_code;
}
public void setProduct_code(int product_code) {
this.product_code = product_code;
}
public int getMrp() {
return mrp;
}
public void setMrp(int mrp) {
this.mrp = mrp;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getProduct_weight() {
return product_weight;
}
public void setProduct_weight(int product_weight) {
this.product_weight = product_weight;
}
public String getProduct_weight_unit() {
return product_weight_unit;
}
public void setProduct_weight_unit(String product_weight_unit) {
this.product_weight_unit = product_weight_unit;
}
public int getDatabase_id() {
return database_id;
}
public void setDatabase_id(int database_id) {
this.database_id = database_id;
}
}
@Dao
public interface StoreDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(List<StoreModel> storeModels);
@Query("DELETE FROM store")
void deleteAll();
@Query("SELECT * FROM store ORDER BY database_id ASC")
DataSource.Factory<Integer,StoreModel>getAllItems();
}
recyclerView.setAdapter(storeAdapter);
store_viewModel.pagedListLiveData.observe(this, new Observer<PagedList<StoreModel>>() {
@Override
public void onChanged(PagedList<StoreModel> storeModels) {
storeAdapter.submitList(storeModels);
//recyclerView.setAdapter(storeAdapter);
}
});
public class ResponseObject{
//@SerializedName("status")
//private int status;
@SerializedName("data")
private List<StoreModel> storeModelList;
//getters and setters goes here
}
public interface Api {
@GET("/get-products")
Call<ResponseObject> getAllProducts();
}