查看其他关于堆栈溢出的问题,有人说这可能是因为valueEventListener的位置不正确,但...不是吗?我正在钻进食物条目/营养条目,但我不知道如何钻进更深。没办法匿名给孩子打电话?我尝试调用childEventListener而不是valueEventListener,其他的都一样,但我再也没有得到那个消息,但我仍然无法检索到我想要的数据。
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
}
编辑:
谢谢你到目前为止的帮助。根据下面Hasan的回答,我更新了onDataChanged中的for循环,这样我得到的是children post的值,而不是DataNapShot,这是一个问题。我还更新了代码,转到正确的UID,然后是nutrition条目,这也会引起问题。我不再收到同样的信息了,但我还是一无所获。当我进入调试器时,没有调用onDataChange,因此resultsList为空。onCancled在更新代码时从未被调用,这里是公开的JSON的屏幕截图,还有我的nutritionEntry类。它有默认构造函数、包含所有参数的构造函数、所有getter和setters以及所有字段与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;
}
}
第二次编辑:
if(userOrAll) {
allEntries = ref.child("Food Entries").orderByChild("foodTitle").equalTo(searchString);
}else{
allEntries = ref.child("Users").child("Nutrition entries").orderByChild("foodTitle").equalTo(searchString);
}
//this is your ref for Nutrition Entries
String UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
allEntries = ref.child("Users").child(UID).child("Nutrition Entries");
现在这一点很重要,请确保在您的类nutritionentry
中的字段与FireBase中的名称匹配:
例如:
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
}
最后读取值:
//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);
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);
}
在身份验证成功时,我正在编写以下代码:
问题内容: 我在输入时搜索用户。但是最后它返回了每个用户。我不知道为什么会这样。好像还好 问题答案: 您正在使用。从文档中: 由返回的实例 将响应节点上具有大于startValue或等于startValue且键大于或等于childKey的值的事件。 因此请记住,如果您搜索某个用户的,则会为所有名称按字母顺序大于给定名称的用户提供。您需要设置a 以限制查询范围。 Firebase旧版文档是了解Fir
我对Firebase和Android是新手。我已经在我的Firebase帐户中存储了用户身份验证详细信息(姓名、个人资料图像而不是电子邮件ID)。我想检索那些数据(如姓名等)到我的应用程序的其他部分。如何检索实时数据库子值?
我试图显示一个零件的"loc",如果它的零件号我给。以下是数据结构的样子: 活动代码: getLoc是Product类的getter函数,它返回给定curP对应的“loc”。curP部分表示子值。 逻辑对我来说似乎是正确的,但我没有得到输出。我哪里出了问题?
firebase数据库最近的更新似乎有一些变化。我的项目在firebase\u数据库:^7.0.0版本下运行良好,但在firebase数据库的更新版本中,我无法更新项目代码。 以下是v7的工作版本 firebase\u数据库^9.0.12上的同一代码出现错误。我收到的错误在`(DataSnapShot snap)async{…}上错误是 无法将参数类型“Future Function(DataSn