2017-08-31 55 views
0

上找到類別名稱沒有設置者/字段我試圖從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" 
} } } 

回答

1

生成由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); 
     } 

似乎與你的模型類的問題,我希望這可以解決您的問題。