[英]How do I pull data from firebase into android studio at the exact point in the code?
Is there any way to pull data from firebase where the code is?有没有办法从代码所在的firebase中提取数据? I currently have valueEventListeners, but they all run after the code below them, thus invalidating the following code.
我目前有 valueEventListeners,但它们都在它们下面的代码之后运行,从而使以下代码无效。 I want to be able to pull a value exactly where the code is, not later.
我希望能够在代码所在的位置提取一个值,而不是稍后。
As of yet, I have not found anything online about this.到目前为止,我还没有在网上找到任何关于此的信息。
A good example of my problems in the code:我在代码中遇到的问题的一个很好的例子:
public void onItemClick(AdapterView<?> l, View v, final int position, long id) {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
final String uid = user.getUid();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
cCESnapshot = dataSnapshot.child(uid).child("currChallenges").child(challengeList.get(position));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Intent intent = new Intent();
intent.setClass(this, ChallengeView.class);
intent.putExtra("snapshot", cCESnapshot.toString());
intent.putExtra("name", challengeList.get(position));
startActivity(intent);
}
cCESnapshot is null because the intent runs before the valueEventListener. cCESnapshot 为空,因为意图在 valueEventListener 之前运行。
The onDataChange()
is asynchronous, so the only way to use the retrieved data is inside onDataChange()
, example: onDataChange()
是异步的,因此使用检索到的数据的唯一方法是在onDataChange()
,例如:
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
cCESnapshot = dataSnapshot.child(uid).child("currChallenges").child(challengeList.get(position));
Intent intent = new Intent();
intent.setClass(this, ChallengeView.class);
intent.putExtra("snapshot", cCESnapshot.toString());
intent.putExtra("name", challengeList.get(position));
startActivity(intent);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
This is an asynchronous operation, when the data will arrive, the onDataChange
callback will be triggered.这是一个异步操作,当数据到达时,将触发
onDataChange
回调。 Your startActivty
code will be executed sequentially and this is why you get no value in cCESnapshot
.您的
startActivty
代码将按顺序执行,这就是您在cCESnapshot
没有任何价值的cCESnapshot
。
Move the startActivity
code inside the listener.在侦听器内移动
startActivity
代码。
But be careful , because each time the onItemClick
click listener will be called, you'll add a value event listener.但是要小心,因为每次调用
onItemClick
单击侦听器时,都会添加一个值事件侦听器。 That way, you'll have multiple calls to onDataChange
in each click and so multiple startActivities
.这样,您将在每次单击中多次调用
onDataChange
以及多个startActivities
。
Instead, i recommend using addListenerForSingleValueEvent
which will be triggered only after a single change in data.相反,我建议使用
addListenerForSingleValueEvent
,它只会在数据发生一次更改后触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.