Retrieve attributes of child from Firebase to android app using ValueEventListener -


structure of database:

enter image description here

i want retrieve details (calories,carbohydrates,fat,protein) in textview when key such "apple" or "orange" entered in plaintext field.

this code i'm using retrieve data app keeps crashing when rn it:

    bdata.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             toast.maketext(diet.this, "here", toast.length_short).show();             final string food_item = etfooditem.gettext().tostring().trim();             toast.maketext(diet.this, food_item, toast.length_short).show();   databasesreference.addvalueeventlistener(new com.google.firebase.database.valueeventlistener() {       @override       public void ondatachange(com.google.firebase.database.datasnapshot datasnapshot) {           (com.google.firebase.database.datasnapshot ds : datasnapshot.child("food_databse").getchildren()) {               getters getter;               getter = (getters) ds.getvalue();               if (food_item == getter.getname()) {                    string abc = getter.getcal();                   toast.maketext(diet.this, abc, toast.length_short).show();                   break;               }else{                   toast.maketext(diet.this, "failed", toast.length_short).show();               }               //getter.getcarb();               //getter.getfat();               //getter.getpro();           }       }        @override       public void oncancelled(databaseerror databaseerror) {        }   });          }       }); 

first recommend adding name field additional key

query foodsquery = database.getreference("food_databse"); foodsquery.addlistenerforsinglevalueevent(new valueeventlistener() {         @override         public void ondatachange(datasnapshot datasnapshot) {             for(datasnapshot postsnapshot : datasnapshot.getchildren()) {                 food food = postsnapshot.getvalue(food.class);                 //don't need if add name child field (see comment in class)                 food.name = postsnapshot.getkey();             }         }          @override         public void oncancelled(databaseerror databaseerror) {          }     } ); 

you may want create food class (in separate file!!)

public class food {     string name = "";     int calories;     int carbohydrates;     int fat;     int protein;      public food() {     }      /*     //uncomment iff add name child field in database     public int getname() {         return id;     }     public void setname(string val) {         this.id = id;     }     */     public int getcalories() {         return id;     }     public void setcalories(int val) {         this.id = id;     }     public int getcarbohydrates() {         return id;     }     public void setcarbohydrates(int val) {         this.id = id;     }     public int getfat() {         return id;     }     public void setfat(int val) {         this.id = id;     }     public int getprotein() {         return id;     }     public void setprotein(int val) {         this.id = id;     } } 

instead of using food class can use

hashmap<string, object> map = (hashmap<string, object>)postsnapshot.getvalue() 

or if values integers:

hashmap<string, integer> map = (hashmap<string, integer>)postsnapshot.getvalue() 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -