我是新手!请有人帮助我。 我知道在这里我正在处理异步编程,我试图返回String但它仍然返回null。
有人可以告诉我哪里错了。
public interface OnGetDataListener {
public void onStart();
public void onSuccess(DataSnapshot data);
public void onFailed(DatabaseError databaseError);
}
public void mReadDataOnce(String email ,String child, final OnGetDataListener listener) {
listener.onStart();
myRef.child(email).child(child).limitToFirst(1).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listener.onSuccess(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
listener.onFailed(databaseError);
}
});
}
private String mCheckInforInServer(String email ,String child) {
final String[] returnvalue = new String[1];
mReadDataOnce(email ,child, new OnGetDataListener() {
@Override
public void onStart() {
//DO SOME THING WHEN START GET DATA HERE
}
@Override
public void onSuccess(DataSnapshot data) {
returnvalue[0] = data.getValue().toString();
//DO SOME THING WHEN GET DATA SUCCESS HERE
}
@Override
public void onFailed(DatabaseError databaseError) {
//DO SOME THING WHEN GET DATA FAILED HERE
}
});
return returnvalue[0] ;
}
然后我称之为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(" ","HIMANSHU" + mCheckInforInServer("a","name"));
Log.i(" ","HIMANSHU" + mCheckInforInServer("a","email"));
}
日志打印为空,我想要值。请帮忙!
答案 0 :(得分:1)
花一点时间,尝试阅读您的代码并理解它。
在方法mCheckInforInServer()
内,代码按以下顺序执行:
returnvalue
OnGetDataListener
类型的匿名实例。OnGetDataListener
类型的实例传递给mReadDataOnce()
方法,并让此方法使用OnGetDataListener
实例随时更改returnvalue
的值(这需要一些时间)完成,因为它使用网络有一些延迟。这个延迟将大于执行下两个语句的延迟,几乎在99.9999%的情况下)mReadDataOnce()
异步操作,程序控件允许它继续执行mCheckInfoInServer()
所以,你只需要达到0.0001%的情况。
或,您可以要求mReadDataOnce()
在主活动完成时调用主要活动中的方法,这样可以在99.9999%的情况下为您提供所需的值,如果一切顺利。你可以尝试这样的事情:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mReadDataOnce("a" ,"email", new OnGetDataListener() {
@Override
public void onStart() {
//DO SOME THING WHEN START GET DATA HERE
}
@Override
public void onSuccess(DataSnapshot data) {
Log.i("", "HIMANSHU" + data.getValue().toString());
//DO SOME THING WHEN GET DATA SUCCESS HERE
}
@Override
public void onFailed(DatabaseError databaseError) {
//DO SOME THING WHEN GET DATA FAILED HERE
}
});
mReadDataOnce("a" ,"email", new OnGetDataListener() {
@Override
public void onStart() {
//DO SOME THING WHEN START GET DATA HERE
}
@Override
public void onSuccess(DataSnapshot data) {
Log.i("", "HIMANSHU" + data.getValue().toString());
//DO SOME THING WHEN GET DATA SUCCESS HERE
}
@Override
public void onFailed(DatabaseError databaseError) {
//DO SOME THING WHEN GET DATA FAILED HERE
}
});
}
代码更新:
实际上,我在这里试图将数据库中的值存储到Records Class。
public interface OnGetDataListener {
public void onStart();
public void onSuccess(DataSnapshot data);
public void onFailed(DatabaseError databaseError);
}
public void mReadDataOnce(String email ,String child, final OnGetDataListener listener) {
listener.onStart();
myRef.child(email).child(child).limitToFirst(1).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listener.onSuccess(dataSnapshot);
record.setName(dataSnapshot.getValue().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
listener.onFailed(databaseError);
}
});
}
然后从onCreate()
中的Records类中检索值mReadDataOnce("a" ,"email", new OnGetDataListener() {
@Override
public void onStart() {
//DO SOME THING WHEN START GET DATA HERE
}
@Override
public void onSuccess(DataSnapshot data) {
Log.i("", "HIMANSHU" + data.getValue().toString());
record.setName(data.getValue().toString());
//DO SOME THING WHEN GET DATA SUCCESS HERE
}
@Override
public void onFailed(DatabaseError databaseError) {
//DO SOME THING WHEN GET DATA FAILED HERE
}
});
Log.i(" ","HIMANSHUKUSHWAHA"+record.getName());
//Printing null