简体   繁体   中英

Searching FireBase realtime database

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:

JSON

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.

First of all don't do this:

if(userOrAll) {
allEntries = ref.child("Food Entries").orderByChild("foodTitle").equalTo(searchString);
}else{
allEntries = ref.child("Users").child("Nutrition entries").orderByChild("foodTitle").equalTo(searchString);
}

Instead decide what the ref you want is, lets say you want to read Users/UID/Nutrition entries/ :

//this is your ref for Nutrition Entries
String UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
allEntries = ref.child("Users").child(UID).child("Nutrition Entries");

Now this is important, make sure that in your class nutritionEntry the fields match the names in the firebase:

For example:

class nutritionEntry{

//these must be named exactly like in the database
String foodTitle;
......
......
......
......

//constructor must be generated for all

//getters and setters must be generated for all

}

Finally read the values:

     //this is your ref for Nutrition Entries
     String UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
     allEntries = ref.child("Users").child(UID).child("Nutrition Entries");

    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren() ){
                nutritionEntry ex = ds.getValue(nutritionEntry.class);
                searchResults.add(ex);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };
    allEntries.addListenerForSingleValueEvent(valueEventListener);

UPDATE:

It seems to me a problem in constructing the object, try this in for loop:

for(DataSnapshot ds : dataSnapshot.getChildren() ){

String date = ds.child("date").getValue(String.class);
String foodTitle = ds.child("foodTitle").getValue(String.class);
int calories = ds.child("calories").getValue(Integer.class);
int protein = ds.child("protein").getValue(Integer.class);
int totalCarb = ds.child("totalCarb").getValue(Integer.class);
int fiber = ds.child("fiber").getValue(Integer.class);
int sugar = ds.child("sugar").getValue(Integer.class);
int totalFat = ds.child("totalFat").getValue(Integer.class);
int trans = ds.child("trans").getValue(Integer.class);
int saturated = ds.child("saturated").getValue(Integer.class);
int unsaturated = ds.child("unsaturated").getValue(Integer.class);
String submittedBy = ds.child("submittedBy").getValue(String.class);


nutritionEntry entry = new nutritionEntry(date,foodTitle,calories,protein,totalCarb,fiber,sugar,totalFat,trans,saturated,unsaturated,submittedBy);

searchResults.add(entry);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM