W/ClassMapper:在类

问题描述:

上找到类别名称没有设置者/字段我试图从Firebase数据库检索数据并将其显示在视图中。当ArrayList中的数据被编码时,代码工作正常。但它显示为空的观点时ArrayList中的数据充满了从火力地堡database。这检索到的数据是什么,我得到的日志:是W/ClassMapper:在类

W/ClassMapper: No setter/field for categoryName found on class 
    W/ClassMapper: No setter/field for categoryImageUrl found on class 
    and so on.. 

这里MainActivity.java(ECartHomeActivity.java):使用(用于获取数据)

  public class ECartHomeActivity extends AppCompatActivity { 
public static final String DATABASE_PATH_UPLOADS = "ProductCategoryModel"; 

// firebase objects 
DatabaseReference databaseMessages; 
ArrayList<ProductCategoryModel> productCategoryList; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_ecart); 

     productCategoryList = new ArrayList<ProductCategoryModel>(); 
    databaseMessages = FirebaseDatabase.getInstance().getReference(DATABASE_PATH_UPLOADS); 

    databaseMessages.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 


      productCategoryList.clear(); 

      for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()){ // iterates through all the messages 

       ProductCategoryModel message = messageSnapshot.getValue(ProductCategoryModel.class); 
       ProductCategoryModel fire = new ProductCategoryModel(); 
       String categoryName = message.getProductCategoryName(); 
       String categoryDescription = message.getProductCategoryDescription(); 
       String categoryDiscount = message.getProductCategoryDiscount(); 
       String categoryImageUrl = message.getProductCategoryImageUrl(); 
       fire.setProductCategoryName(categoryName); 
       fire.setProductCategoryDescription(categoryDescription); 
       fire.setProductCategoryDiscount(categoryDiscount); 
       fire.setProductCategoryImageUrl(categoryImageUrl); 
       productCategoryList.add(fire); 
      } 




      CenterRepository.getCenterRepository().setListOfCategory(productCategoryList); 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); } 

如果“productCategoryList”ArrayList是硬编码的,它将在视图中显示数据。 但是,如果此ArrayList填充了从数据库检索的数据,则不会显示任何数据。

ProductCategoryModel.java

public class ProductCategoryModel { 



public ProductCategoryModel(){ 

} 


private String categoryName; 
private String categoryDescription; 
private String categoryDiscount; 
private String categoryImageUrl; 



/** 
* @param productCategoryName 
* @param productCategoryDescription 
* @param productCategoryDiscount 
* @param productCategoryUrl 
*/ 
public ProductCategoryModel(String productCategoryName, String productCategoryDescription, 
     String productCategoryDiscount, String productCategoryUrl) { 
    super(); 
    this.categoryName = productCategoryName; 
    this.categoryDescription = productCategoryDescription; 
    this.categoryDiscount = productCategoryDiscount; 
    this.categoryImageUrl = productCategoryUrl; 
} 







/** 
* @return the idproductcategory 
*/ 
public String getProductCategoryName() { 
    return categoryName; 
} 

/** 
* @param categoryName 
*   the idproductcategory to set 
*/ 
public void setProductCategoryName(String categoryName) { 
    this.categoryName = categoryName; 
} 





/** 
* @return the productDescription 
*/ 
public String getProductCategoryDescription() { 
    return categoryDescription; 
} 

/** 
* @param categoryDescription 
*   the productDescription to set 
*/ 
public void setProductCategoryDescription(String categoryDescription) { 
    this.categoryDescription = categoryDescription; 
} 




/** 
* @return the productDiscount 
*/ 
public String getProductCategoryDiscount() { 
    return categoryDiscount; 
} 

/** 
* @param categoryDiscount 
*   the productDiscount to set 
*/ 
public void setProductCategoryDiscount(String categoryDiscount) { 
    this.categoryDiscount = categoryDiscount; 
} 





/** 
* @return the productUrl 
*/ 
public String getProductCategoryImageUrl() { 
    return categoryImageUrl; 
} 

/** 
* @param categoryImageUrl 
*   the productUrl to set 
*/ 
public void setProductCategoryImageUrl(String categoryImageUrl) { 
    this.categoryImageUrl = categoryImageUrl; 
} } 

这是JSON代码:

{ 
    "ProductCategoryModel" : { 
"1" : { 
    "categoryName" : "anonymous", 
    "categoryDescription" : "Heyyyy", 
    "categoryDiscount" : "anonymous", 
    "categoryImageUrl" : "anonymous" 
}, 
"2" : { 
"categoryName" : "anonymous", 
    "categoryDescription" : "Heyyyy", 
    "categoryDiscount" : "anonymous", 
    "categoryImageUrl" : "anonymous" 
}, 
"3" : { 
    "categoryName" : "anonymous", 
    "categoryDescription" : "Heyyyy", 
    "categoryDiscount" : "anonymous", 
    "categoryImageUrl" : "anonymous" 
} } } 

生成由Android工作室的默认快捷方式ProductCategoryModel类的getter和setter(和构造函数)(ALT +插入或右键单击并生成),因为在手动输入时可能会出现某些字母的错误,您可以替换

for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()){ // iterates through all the messages 
      ProductCategoryModel message = messageSnapshot.getValue(ProductCategoryModel.class); 
      ProductCategoryModel fire = new ProductCategoryModel(); 
      String categoryName = message.getProductCategoryName(); 
      String categoryDescription = message.getProductCategoryDescription(); 
      String categoryDiscount = message.getProductCategoryDiscount(); 
      String categoryImageUrl = message.getProductCategoryImageUrl(); 
      fire.setProductCategoryName(categoryName); 
      fire.setProductCategoryDescription(categoryDescription); 
      fire.setProductCategoryDiscount(categoryDiscount); 
      fire.setProductCategoryImageUrl(categoryImageUrl); 
      productCategoryList.add(fire); 
     } 

这对一些聪明的办法:

for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()){ // iterates through all the messages 

      String categoryName = message.getProductCategoryName(); 
      String categoryDescription = message.getProductCategoryDescription(); 
      String categoryDiscount = message.getProductCategoryDiscount(); 
      String categoryImageUrl = message.getProductCategoryImageUrl(); 
      ProductCategoryModel fire = new ProductCategoryModel(categoryName,categoryDescription,categoryDiscount,categoryImageUrl); 
      productCategoryList.add(fire); 
     } 

似乎与你的模型类的问题,我希望这可以解决您的问题。