英文:
Firebase Datasnapshot returns null value
问题
我想从这个节点中检索这个值("id"),但我得到的值是null。我已经搜索了很多解决方案,可能与异步方式或其他什么有关,我猜?
这是数据库,突出显示的节点是我想要获取的值:
这是我的代码:
reference = FirebaseDatabase.getInstance().getReference();
id = null;
Query lastQuery = reference.child("Donation Request").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.child("id").exists())
{
id = dataSnapshot.child("id").getValue().toString();
int index = Integer.parseInt(id) + 1;
id = Integer.toString(index);
Toast.makeText(getApplicationContext(), "It works!!!", Toast.LENGTH_SHORT).show();
}
else
{
id = "1";
Toast.makeText(getApplicationContext(), "It doesn't work.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
如果有人能帮助我解决这个问题,将不胜感激!
英文:
I want to retrieve this value from this node ("id"), and the value i get is null. I have googled so many solutions that this might have to do with asynchronous way or something, i guess?
This is the database, and the highlighted node is the value i would like to get:
This is my code:
reference = FirebaseDatabase.getInstance().getReference();
id = null;
Query lastQuery = reference.child("Donation Request").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.child("id").exists())
{
id = dataSnapshot.child("id").getValue().toString();
int index = Integer.parseInt(id) + 1;
id = Integer.toString(index);
Toast.makeText(getApplicationContext(), "It works!!!", Toast.LENGTH_SHORT).show();
}
else
{
id = "1";
Toast.makeText(getApplicationContext(), "It doesn't work.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
Most appreciated if someone can help me out of this!
答案1
得分: 1
当您对Firebase数据库执行查询时,可能会得到多个结果。因此,快照包含了这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。
您的onDataChange
需要通过循环遍历dataSnapshot.getChildren()
来处理这个列表:
reference = FirebaseDatabase.getInstance().getReference();
id = null;
Query lastQuery = reference.child("Donation Request").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
if (snapshot.hasChild("id"))
{
id = snapshot.child("id").getValue(String.class);
int index = Integer.parseInt(id) + 1;
id = Integer.toString(index);
Toast.makeText(getApplicationContext(), "It works!!!", Toast.LENGTH_SHORT).show();
}
else
{
id = "1";
Toast.makeText(getApplicationContext(), "It doesn't work.", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
throw databaseError.toException(); // 永远不要忽视错误。
}
});
另外注意:
- 任何对
id
的使用都需要在onDataChange
内部进行,或者从其中调用。在外部,您无法保证id
将被赋予您所期望的值。 - 使用吐司(Toast)进行调试可能会变得混乱。我强烈建议使用
Log.d(...)
等方法,研究应用程序的logcat输出中的输出(及其顺序)。
英文:
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
Your onDataChange
needs to handle this list by looping over dataSnapshot.getChildren())
:
reference = FirebaseDatabase.getInstance().getReference();
id = null;
Query lastQuery = reference.child("Donation Request").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
if (snapshot.hasChild("id"))
{
id = snapshot.child("id").getValue(String.class);
int index = Integer.parseInt(id) + 1;
id = Integer.toString(index);
Toast.makeText(getApplicationContext(), "It works!!!", Toast.LENGTH_SHORT).show();
}
else
{
id = "1";
Toast.makeText(getApplicationContext(), "It doesn't work.", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
throw databaseError.toException(); // never ignore errors.
}
});
A few more notes:
- Any use of
id
needs to happen insideonDataChange
, or be called from there. Outside of that, you won't have any guarantees thatid
will have been assigned the value you expect. - Using toasts for debugging is bound to become confusing. I highly recommend using
Log.d(...)
and friends, and studying the output (and its order) in the logcat output of your app.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论