new to firebase real-time and I'm trying to make an android Macro tracking app. When I try to run the code below I get "No setter/field for -M6v0ZiU6tLuBDlmcuEe found on class com.example.pttracker.nutritionEntry"
Looking through other questions on stack overflow someone said this could be because valueEventListener isn't positioned correctly but...isn't it? I am drilling down to food entries/nutrition entries, but I don't know how to drill deeper than that. There's no method to call children anonymously? I tried calling childEventListener instead of valueEventListener, with all else the same, and I didn't get that message anymore but I still couldn't retrieve the data I wanted.
My JSON Structure:
My code:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query allEntries = ref.child("Users").child(profile.getProfileKey()).child("Nutrition entries").orderByChild("foodTitle").equalTo(search);
// Query for all entries with a certain child with value equal to something
final ArrayList<nutritionEntry> searchResults = new ArrayList<nutritionEntry>();
// Add listener for Firebase response on said query
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot post : dataSnapshot.getChildren() ){
nutritionEntry ex = post.getValue(nutritionEntry.class);
searchResults.add(ex);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("Error", "Search Cancled");
}
};
allEntries.addListenerForSingleValueEvent(valueEventListener);
if(searchResults.isEmpty()){
TableRow tr = new TableRow(nutritionTracker.this);
TextView tv1 = new TextView(nutritionTracker.this);
tv1.setText("No Results");
}else{
for(final nutritionEntry e : searchResults) {
//code to display the entries
}
Any help and explanation is greatly appreciated.
EDIT:
Thank you for the help so far. Per Hasan's answer below I updated the for loop in onDataChanged so that I am getting value of children post instead of datasnapshot, that was one problem. I also updated the code to go to the proper UID, then nutrition entries, which would have cause problems too. I don't get the same message anymore but I still get nothing. When I go into debugger, onDataChange is not called so the resultsList is empty. onCancled is never called either Updating code and here is a screenshot of exposed JSON, as well as my nutritionEntry class. It has default constructor, constructor with all params, all getters and setters, and all fields match what is in firebase:
public class nutritionEntry {
public String date;
public String foodTitle;
public int calories;
public int protein;
public int totalCarb;
public int fiber;
public int sugar;
public int totalFat;
public int trans;
public int saturated;
public int unsaturated;
public String submittedBy;
public nutritionEntry(String date,String foodTitle, int calories, int protein, int totalCarb, int fiber, int sugar, int totalFat, int trans, int saturated, int unsaturated, String submitter) {
this.date = date;
this.foodTitle = foodTitle;
this.calories = calories;
this.protein = protein;
this.totalCarb = totalCarb;
this.fiber = fiber;
this.sugar = sugar;
this.totalFat = totalFat;
this.trans = trans;
this.saturated = saturated;
this.unsaturated = unsaturated;
this.submittedBy = submitter;
}
public nutritionEntry() {}
public String getDate() {
return date;
}
public String getFoodTitle() {
return foodTitle;
}
public int getCalories() {
return calories;
}
public int getProtein() {
return protein;
}
public int getTotalCarb() {
return totalCarb;
}
public int getFiber() {
return fiber;
}
public int getSugar() {
return sugar;
}
public int getTotalFat() {
return totalFat;
}
public int getTrans() {
return trans;
}
public int getSaturated() {
return saturated;
}
public int getUnsaturated() {
return unsaturated;
}
public String getSubmittedBy() {
return submittedBy;
}
public void setDate(String date) {
this.date = date;
}
public void setFoodTitle(String foodTitle) {
this.foodTitle = foodTitle;
}
public void setCalories(int calories) {
this.calories = calories;
}
public void setProtein(int protein) {
this.protein = protein;
}
public void setTotalCarb(int totalCarb) {
this.totalCarb = totalCarb;
}
public void setFiber(int fiber) {
this.fiber = fiber;
}
public void setSugar(int sugar) {
this.sugar = sugar;
}
public void setTotalFat(int totalFat) {
this.totalFat = totalFat;
}
public void setTrans(int trans) {
this.trans = trans;
}
public void setSaturated(int saturated) {
this.saturated = saturated;
}
public void setUnsaturated(int unsaturated) {
this.unsaturated = unsaturated;
}
public void setSubmittedBy(String submittedBy) {
this.submittedBy = submittedBy;
}
}
2ND EDIT:
For now my database rules are read: true, and write: true so i have permissions.
FINAL EDIT:
After much troubleshooting various problems below with Hasan the final piece of the puzzle was the fact that both my variables and my getters/setters were declared public in my nutritionEntry class. Changning the variables from public to default access made it so that I could finally retrieve entries from Firebase.

