我的数据库结构:
我想在明文字段中输入诸如“Apple”或“Orange”之类的键时,在Textview中检索所有细节(卡路里,碳水化合物,脂肪,蛋白质)。
这是我用来检索数据的代码,但是当我使用它时,我的应用程序一直在崩溃:
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) {
for (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) {
}
});
}
});
答案 0 :(得分:1)
为了解决您的问题,请使用以下代码:
EditText editText = (EditText) findViewById(R.id.edit_text);
String fruit = editText.getText().toString();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference fruitRef = rootRef.child("Food-Databse").child(fruit);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String calories = ds.child("Calories").getValue(String.class);
String carbohydrates = ds.child("Carbohydrates").getValue(String.class);
String fat = ds.child("Fat").getValue(String.class);
String protein = ds.child("Protein").getValue(String.class);
Log.d("TAG", calories + " / " + carbohydrates + " / " + fat + " / " + protein);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
fruitRef.addListenerForSingleValueEvent(eventListener);
请注意我在屏幕截图中使用的Food-Databse
,而不是正常情况下的Food-Database
。
希望它有所帮助。
答案 1 :(得分:0)
首先我建议在密钥
之外添加一个名称字段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 it if you add name child field (see comment in class)
food.name = postSnapshot.getKey();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
}
);
您可能想要创建一个Food类(在单独的文件中!!)
public class Food {
String name = "";
int calories;
int carbohydrates;
int fat;
int protein;
public Food() {
}
/*
//uncomment iff you 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;
}
}
而不是使用Food类,你可以使用
HashMap<String, Object> map = (HashMap<String, Object>)postSnapshot.getValue()
或者如果所有值都是整数:
HashMap<String, Integer> map = (HashMap<String, Integer>)postSnapshot.getValue()