如何在继续之前等待非空 return

How to wait for non null return before continuing

我想从单独的 class 中检索位图。单独的 class 有一个方法可以通过数据回调从在线服务器下载位图。默认情况下,方法 returns null 直到下载位图。当我从 main class 调用该方法时,我得到 null。在继续任何主要 class 操作之前,如何等待此检索非空(即位图已下载)?

主要Class

profileBitmap = myParse.getImageBitmap(getContext(), tagId);

分开Class

public Bitmap getImageBitmap (final Context context, String tagId) {

        // give us the user's photo
        ParseQuery<ParseObject> parseQuery = new ParseQuery<>("Photo");
        parseQuery.whereEqualTo("username", tagId);
        parseQuery.getFirstInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                if (object != null && e == null) {
                    ParseFile profilePic = (ParseFile) object.get("profilePicture");
                    //download process
                    profilePic.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] data, ParseException e) {
                            if (data != null && e == null) {
                                // converts file to image
                                selectedImageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                            } else {
                                // returns error user bitmap
                                selectedImageBitmap = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.ic_dialog_alert);
                                FancyToast.makeText(context, "Error: " + e.getMessage(), FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
                            }
                        }
                    });
                } else {
                    // returns empty user bitmap
                    selectedImageBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.empty_user);
                    FancyToast.makeText(context, "Error: " + e.getMessage(), FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
                }
            }
        });
        return selectedImageBitmap;
    } 

我会使用 listener:

在新文件中创建一个interface

public interface onBitmapDownloadedListener{

void onDownloadFinished(Bitmap bitmap);

}

在您的主 class 中实现 interface,并将您的主 class 的引用传递给 MyParse class

class MainClass implements onBitmapDownloadedListener{


//now when you create an instance of myParse, pass the listener
......

myParse = new MyParse(this);

@Override
public void onDownloadFinished(Bitmap bitmap){

//here you get the bitmap

profileBitmap = bitmap

}

MyParse class 中创建接受接口的构造函数:

class MyParse {

private onBitmapDownloadedListener listener;


//constrcutor
public MyParse(onBitmapDownloadedListener listener){

this.listener = listener

}

//your method
public Bitmap getImageBitmap (final Context context, String tagId) {
........
........
........

@Override
public void done(byte[] data, ParseException e) {
if (data != null && e == null) {
// converts file to image
selectedImageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

//notify here
listener.onDownloadFinished(selectedImageBitmap);

}
....
....
....


}


}