Solution 1 :
The property (fields and getter/setter) names in your Java class don’t match up with the ones in your database. You can explicitly map the class to the database with Java annotations:
public class CategoryModel {
private String categoryIconLink;
private String categoryName;
public CategoryModel() { }
public CategoryModel(String categoryIconLink, String categoryName) {
this.categoryIconLink = categoryIconLink;
this.categoryName = categoryName;
}
@PropertyName("img")
public String getCategoryIconLink() {
return categoryIconLink;
}
@PropertyName("name")
public String getCategoryName() {
return categoryName;
}
@PropertyName("img")
public void setCategoryIconLink(String categoryIconLink) {
this.categoryIconLink = categoryIconLink;
}
@PropertyName("name")
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
}
And you also need a no-argument constructor, as otherwise Firebase doesn’t know how to instantiate your class. So that’s the public CategoryModel() { }
I added.
Problem :
I stuck on one point please help me to solve this I need to send cateModelList to my adaptor in required format which consists of parameters 1) String type link 2) String type category name
I got data from firebase, I did not understand how can I format this data into my required list in for loop please help me
List<CategoryModel> categoryModelList = new ArrayList<CategoryModel>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = database.getReference("categories");
// Read from the database
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data:dataSnapshot.getChildren()){
categoryModelList.add(new CategoryModel(data.child().getValue("img"), data.child().getValue("name")))
}
}
@Override
public void onCancelled(DatabaseError error) {}
});
Modal class
package com.e.mymallapp.ui.home;
public class CategoryModel {
private String categoryIconLink;
private String categoryName;
public CategoryModel(String categoryIconLink, String categoryName) {
this.categoryIconLink = categoryIconLink;
this.categoryName = categoryName;
}
public String getCategoryIconLink() {
return categoryIconLink;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryIconLink(String categoryIconLink) {
this.categoryIconLink = categoryIconLink;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
}
firebase DB structure
Needed Output
List<CategoryModel> categoryModelList = new ArrayList<CategoryModel>();
categoryModelList.add(new CategoryModel("link","Home"));
categoryModelList.add(new CategoryModel("link","Electronics"));
categoryModelList.add(new CategoryModel("link","Essentials"));
categoryModelList.add(new CategoryModel("link","Grocery"));
categoryModelList.add(new CategoryModel("link","Mobile"));
categoryModelList.add(new CategoryModel("link","Fashion"));
categoryModelList.add(new CategoryModel("link","Beauty"));
categoryModelList.add(new CategoryModel("link","Appliances"));
categoryModelList.add(new CategoryModel("link","Toys & Baby"));
categoryModelList.add(new CategoryModel("link","Flight"));
categoryModelList.add(new CategoryModel("link","Sports"));
Guys please help me I did not understand how can I get this type of list from this data