[英]Problem implementing interface in an onComplete method
Following the previous thread that I have created here: How do I set a variable inside of an onComplete method?按照我在此处创建的上一个主题: 如何在 onComplete 方法中设置变量?
I am still trying to call the variable that I have set inside on the onComplete
method.我仍在尝试调用我在
onComplete
方法中设置的变量。 I know there is a delay in-between accessing the method.我知道访问该方法之间存在延迟。 And the one solution that was brought up to me was to create an interface.
向我提出的一个解决方案是创建一个界面。 Although, I have successfully implemented the interface, I am not sure whether my implementation is correct since I am still getting nulls.
虽然,我已经成功实现了接口,但我不确定我的实现是否正确,因为我仍然得到空值。
Here's my Interface:这是我的界面:
public interface MyCallBack {
void onCallFullName(String fullName);
void onCallEmail(String Email);
}
The method to fetch the data from firebase:从firebase取数据的方法:
private void callUserCredential(MyCallBack myCallBack) {
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
String userID = fAuth.getCurrentUser().getUid();
DocumentReference docRef = fStore
.collection("users")
.document(userID);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if(document.exists()){
//Log for successfully fetching data from firebase
Log.d(TAG,"User full name: " + document.getString("FullName"));
Log.d(TAG, "User email: " + document.getString("Email"));
String fullName = document.getString("FullName");
String eMail = document.getString("Email");
//Log for successfully setting the data into the variables
Log.d(TAG, "donorFullName set: " + fullName);
Log.d(TAG, "donorEmail set: " + eMail);
//Interface callback
myCallBack.onCallFullName(fullName);
myCallBack.onCallEmail(eMail);
}
}
}
});
}
and Lastly, here's me calling the Interface:最后,这是我调用接口:
//Create booking information
BookingInformation bookingInformation = new BookingInformation();
callUserCredential(new MyCallBack() {
@Override
public void onCallFullName(String fullName) {
donorFullName = fullName;
}
@Override
public void onCallEmail(String Email) {
donorEmail = Email;
}
});
//Log for successfully calling the interface
Log.d(TAG, "donorFullName bookingInformation: " + donorFullName);
Log.d(TAG, "donorEmail bookingInformation: " + donorEmail);
bookingInformation.setDonorName(donorFullName);
bookingInformation.setDonorEmail(donorEmail);
The logcat still shows the same thing: logcat 仍然显示相同的内容:
You must setDonorName() and setDonorEmail() in Callback functions like:您必须在回调函数中设置 DonorName() 和 setDonorEmail(),例如:
//Create booking information
BookingInformation bookingInformation = new BookingInformation();
callUserCredential(new MyCallBack() {
@Override
public void onCallFullName(String fullName) {
bookingInformation.setDonorName(fullName);
//Log for successfully calling the interface
Log.d(TAG, "donorFullName bookingInformation: " + fullName);
}
@Override
public void onCallEmail(String email) {
bookingInformation.setDonorEmail(email);
//Log for successfully calling the interface
Log.d(TAG, "donorEmail bookingInformation: " + email);
}
});
Because when you call the log function, the request is uncompleted yet!因为当你调用日志function时,请求还没有完成!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.