我有下一个方法,返回 ArrayList<String>
,来自 Google 云端硬盘帐户的数据。
public int getMetodsList()
{ final ArrayList<String> result = new ArrayList<String>();
System.out.println("1");
Drive.DriveApi.getFolder(MainActivity.mGoogleApiClient, getIDOfFolder("SomeFolder"))
.listChildren(MainActivity.mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>()
{
@Override
public void onResult(MetadataBufferResult mdbr) {
System.out.println("2");
for (int i = 0; i < mdbr.getMetadataBuffer().getCount(); i++)
{ result.add(mdbr.getMetadataBuffer().get(i).getTitle());
}
System.out.println("3");
} }
);
System.out.println("4");
return result.size();
为了帮助我添加了 log
带数字 - System.out.println("number");
当我调用此方法时,Log
返回我的下一个序列:
1、4、2、3。
所以我做错了ArrayList
, return 在 CallBack()
之前调用
请帮忙,我如何调用回调 ** ResultCallback<DriveApi.MetadataBufferResult>
之前return
?
最佳答案
您使用的是异步方法,因此输出不足为奇。当你想同步访问文件夹内容时,使用await
:
With this release of Google Play services, you now have the option to specify if an API call should execute asynchronously (you will receive a callback once it is finished), or synchronously (the thread will block until the operation has completed). This is achieved by using the classes PendingResult, Result, and Status in the com.google.android.gms.common.api package.
In practice, this means that API operations will return an instance of PendingResult, and you can choose if you want the method to execute asynchronously using setResultCallback or synchronously using await. (http://android-developers.blogspot.de/2014/02/new-client-api-model-in-google-play.html).
关于java - 带有回调的方法中的操作顺序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22474791/