我想显示从RecyclerView行项目上的Retrofit调用获得的值。我解析JSON对象,并将其存储在Sqlite DB中。我可以设法在TextView上显示5个字符串值中的2个,但是,无法弄清楚为什么其余的都不显示。
下面是我的活动,适配器,模型和数据库类以及行项目的布局,请注意。 您能帮我解决这个问题吗?
非常感谢您!
ActivityClass
public class InventoryProductActivity extends AppCompatActivity implements
InventoryProductListAdapter.CustomClickListener {
private static final String TAG =
InventoryProductActivity.class.getSimpleName();
private InventoryProductListAdapter mInventoryProductListAdapter;
private RecyclerView mRecyclerView;
private RetrofitClient mRetrofitClient;
LinearLayoutManager mLinearLayoutManager;
private WarehouseDatabase mDatabase;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory_product);
configViews();
mRetrofitClient = new RetrofitClient();
mDatabase = new WarehouseDatabase(this);
loadInventoryProductFeed();
}
private void configViews() {
mRecyclerView = findViewById(R.id.recycler_view_inventory_product);
mRecyclerView.setHasFixedSize(true);
mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mInventoryProductListAdapter = new InventoryProductListAdapter(this);
mRecyclerView.setAdapter(mInventoryProductListAdapter);
}
private void loadInventoryProductFeed() {
mProgressDialog = new ProgressDialog(InventoryProductActivity.this);
mProgressDialog.setMessage("Loading Inventory Data...");
mProgressDialog.setCancelable(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setIndeterminate(true);
mProgressDialog.show();
mInventoryProductListAdapter.reset();
if (getNetworkAvailability()) {
getFeed();
} else {
getFeedFromDatabase();
}
}
private void getFeed() {
Call<InventoryProductModel> call =
mRetrofitClient.getWarehouseServiceInventoryProduct().getAllInventoryProducts();
call.enqueue(new Callback<InventoryProductModel>() {
@Override
public void onResponse(Call<InventoryProductModel> call, Response<InventoryProductModel> response) {
if (response.isSuccessful()) {
response.body();
String inventoryCode = response.body().getCode().toString(); //1
String productName = response.body().getName().toString(); //2
String groupID = response.body().getGroupId().toString(); //4
String subGroupID = response.body().getGroupId().toString(); //6
String typeID = response.body().getGroupId().toString(); //8
String producerID = response.body().getProducerId().toString(); //10
String inventoryClass = response.body().getInvclass().toString();//11
String categoryID = response.body().getCategoryID().toString();//13
String countryID = response.body().getCountryId().toString(); //15
//TODO Below are null attributes. Empty responses from server.
//TODO Find a soloution to handle them or else remove them from db and dont process
/*String group = response.body().getGroup().toString(); //3
String subGroup = response.body().getSubGroup().toString(); //5
String type = response.body().getGroupId().toString(); //7
String producer = response.body().getProducer().toString();//9
String category = response.body().getCategory().toString(); //12
String country = response.body().getCountry().toString();//14*/
List<InventoryProductModel> responseList = new ArrayList<>();
responseList.add(new InventoryProductModel(inventoryCode.toString(),productName.toString(),
groupID.toString(), subGroupID.toString(),typeID.toString(),producerID.toString(),
inventoryClass.toString(),categoryID.toString(),countryID.toString()));
for (int i=0; i<responseList.size(); i++){
InventoryProductModel inventoryProductModel = responseList.get(i);
mInventoryProductListAdapter.addInventoryProduct(inventoryProductModel);
}
mInventoryProductListAdapter.notifyDataSetChanged();
} else {
int sc = response.code();
switch (sc) {
}
}
mProgressDialog.dismiss();
}
@Override
public void onFailure(Call<InventoryProductModel> call, Throwable t) {
}
});
}
private void getFeedFromDatabase() {
List<InventoryProductModel> inventoryProductModelList = mDatabase.getInventoryProducts();
for (int i = 0; i < inventoryProductModelList.size(); i++) {
InventoryProductModel inventoryProductModel = inventoryProductModelList.get(i);
Log.d(TAG, inventoryProductModel.getName() + "||" + inventoryProductModel.getCountryId());
}
mProgressDialog.dismiss();
}
private boolean getNetworkAvailability() {
return Utils.isNetworkAvailable(getApplicationContext());
}
@Override
public void onClick(int position) {
}
}
适配器
public class InventoryProductListAdapter extends RecyclerView.Adapter<InventoryProductListAdapter.MyViewHolder> {
private static final String TAG = InventoryProductListAdapter.class.getSimpleName();
private List<InventoryProductModel> myInventoryProductList;
private final CustomClickListener mCustomClickListener;
public InventoryProductListAdapter(CustomClickListener customClickListener) {
myInventoryProductList = new ArrayList<>();
mCustomClickListener = customClickListener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_inventory_product, null, false);
return new MyViewHolder(row);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
InventoryProductModel currentInventoryProduct = myInventoryProductList.get(position);
holder.mProductName.setText(currentInventoryProduct.getName());
holder.mInventoryCode.setText(currentInventoryProduct.getCode());
holder.mInventoryClass.setText(currentInventoryProduct.getInvclass());
holder.mCategoryID.setText(currentInventoryProduct.getCategoryID());
holder.mCountryID.setText(currentInventoryProduct.getCountryId());
}
@Override
public int getItemCount() {
return myInventoryProductList.size();
}
public void addInventoryProduct (InventoryProductModel inventoryProductModel){
myInventoryProductList.add(inventoryProductModel);
}
public InventoryProductModel getSelectedInventoryProduct(int position){
return myInventoryProductList.get(position);
}
public void reset() {
myInventoryProductList.clear();
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mThumbnail;
private TextView mProductName, mInventoryCode, mInventoryClass, mCategoryID, mCountryID;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mThumbnail = itemView.findViewById(R.id.thumbnail);
mProductName = itemView.findViewById(R.id.product_name);
mInventoryCode = itemView.findViewById(R.id.inventory_code);
mInventoryClass = itemView.findViewById(R.id.inventory_class);
mCategoryID = itemView.findViewById(R.id.category_id);
mCountryID = itemView.findViewById(R.id.country_id);
}
@Override
public void onClick(View v) {
mCustomClickListener.onClick(getLayoutPosition());
}
}
public interface CustomClickListener {
void onClick(int position);
}
}
模型
public class InventoryProductModel {
@SerializedName("code")
@Expose
private String code;
@SerializedName("name")
@Expose
private String name;
@SerializedName("group")
@Expose
private String group;
//private Object group;
@SerializedName("groupId")
@Expose
private String groupId;
@SerializedName("subGroup")
@Expose
private String subGroup;
//private Object subGroup;
@SerializedName("subGroupId")
@Expose
private String subGroupId;
@SerializedName("type")
@Expose
private String type;
//private Object type;
@SerializedName("typeId")
@Expose
private String typeId;
@SerializedName("producer")
@Expose
private String producer;
//private Object producer;
@SerializedName("producerId")
@Expose
private String producerId;
@SerializedName("invclass")
@Expose
private String invclass;
@SerializedName("category")
@Expose
private String category;
//private Object category;
@SerializedName("categoryID")
@Expose
private String categoryID;
@SerializedName("country")
@Expose
private String country;
//private Object country;
@SerializedName("countryId")
@Expose
private String countryId;
public InventoryProductModel(String code, String name, String group, String groupId, String subGroup,
String subGroupId, String type, String typeId, String producer) {
this.code = code;
this.name = name;
this.group = group;
this.groupId = groupId;
this.subGroup = subGroup;
this.subGroupId = subGroupId;
this.type = type;
this.typeId = typeId;
this.producer = producer;
this.producerId = producerId;
this.invclass = invclass;
this.category = category;
this.categoryID = categoryID;
this.country = country;
this.countryId = countryId;
}
public InventoryProductModel() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getSubGroup() {
return subGroup;
}
public void setSubGroup(String subGroup) {
this.subGroup = subGroup;
}
public String getSubGroupId() {
return subGroupId;
}
public void setSubGroupId(String subGroupId) {
this.subGroupId = subGroupId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTypeId() {
return typeId;
}
public void setTypeId(String typeId) {
this.typeId = typeId;
}
public String getProducer() {
return producer;
}
public void setProducer(String producer) {
this.producer = producer;
}
public String getProducerId() {
return producerId;
}
public void setProducerId(String producerId) {
this.producerId = producerId;
}
public String getInvclass() {
return invclass;
}
public void setInvclass(String invclass) {
this.invclass = invclass;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getCategoryID() {
return categoryID;
}
public void setCategoryID(String categoryID) {
this.categoryID = categoryID;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountryId() {
return countryId;
}
public void setCountryId(String countryId) {
this.countryId = countryId;
}
}
数据库
public class WarehouseDatabase extends SQLiteOpenHelper {
private final String TAG = WarehouseDatabase.class.getSimpleName();
public WarehouseDatabase(Context context) {
super(context, Constants.DATABASE.DB_NAME, null, Constants.DATABASE.DB_VERSION);
}
//Creating Required Tables
@Override
public void onCreate(SQLiteDatabase db) {
try {
..
db.execSQL(Constants.DATABASE.CREATE_QUERY_TABLE_INVENTORY_PRODUCT);
..
} catch (SQLException ex) {
Log.d(TAG, ex.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
..
db.execSQL(Constants.DATABASE.DROP_QUERY_INVENTORY_PRODUCT);
..
this.onCreate(db);
}
//CRUD Operations
public void addInventoryProduct(InventoryProductModel inventoryProductModel) {
Log.d(TAG, "Values got: " + inventoryProductModel.getName());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_BARCODE, inventoryProductModel.getCode());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_NAME, inventoryProductModel.getName());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_GROUP, inventoryProductModel.getGroup());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_GROUP_ID, inventoryProductModel.getGroupId());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_SUB_GROUP, inventoryProductModel.getSubGroup());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_SUB_GROUP_ID, inventoryProductModel.getSubGroupId());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_TYPE, inventoryProductModel.getType());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_TYPE_ID, inventoryProductModel.getTypeId());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_PRODUCER, inventoryProductModel.getProducer());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_PRODUCER_ID, inventoryProductModel.getProducerId());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_INVCLASS, inventoryProductModel.getInvclass());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_CATEGORY, inventoryProductModel.getCategory());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_CATEGORY_ID, inventoryProductModel.getCategoryID());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_COUNTRY, inventoryProductModel.getCountry());
values.put(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_COUNTRY_ID, inventoryProductModel.getCountryId());
try {
db.insert(Constants.DATABASE.TABLE_INVENTORY_PRODUCT, null, values);
} catch (Exception e) {
}
db.close();
}
//GET,FETCH ITEMS AS LIST
public List<InventoryProductModel> getInventoryProducts() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(Constants.DATABASE.GET_QUERY_INVENTORY_PRODUCT, null);
List<InventoryProductModel> listInventoryProduct = new ArrayList<>();
if (cursor.moveToFirst()) {
//if there is any item
do {
InventoryProductModel inventoryProductModel = new InventoryProductModel();
inventoryProductModel.setCode(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_BARCODE)));
inventoryProductModel.setName(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_NAME)));
inventoryProductModel.setGroup(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_GROUP)));
inventoryProductModel.setGroupId(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_GROUP)));
inventoryProductModel.setSubGroup(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_SUB_GROUP)));
inventoryProductModel.setSubGroupId(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_SUB_GROUP_ID)));
inventoryProductModel.setType(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_TYPE)));
inventoryProductModel.setTypeId(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_TYPE_ID)));
inventoryProductModel.setProducer(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_SUB_GROUP_ID)));
inventoryProductModel.setProducerId(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_PRODUCER_ID)));
inventoryProductModel.setInvclass(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_INVCLASS)));
inventoryProductModel.setCategory(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_CATEGORY)));
inventoryProductModel.setCategoryID(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_CATEGORY_ID)));
inventoryProductModel.setCountry(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_COUNTRY)));
inventoryProductModel.setCountryId(cursor.getString(cursor.getColumnIndex(Constants.DATABASE.COLUMN_INVENTORY_PRODUCT_COUNTRY_ID)));
} while (cursor.moveToNext());
}
return listInventoryProduct;
}
}
行布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:orientation="horizontal"
android:padding="8dp">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="@drawable/img_loading_shape" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product name"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/inventory_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Inventory Code" />
<TextView
android:id="@+id/inventory_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Inventory Class" />
<TextView
android:id="@+id/category_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Category ID" />
<TextView
android:id="@+id/country_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Country ID" />
</LinearLayout>
</LinearLayout>