简体   繁体   English

在asynctask之前显示对话框

[英]show dialog before asynctask

I'm trying to show a "Loading" dialog before the async task starts working. 我正在尝试在异步任务开始工作之前显示“正在加载”对话框。 With the following code what happens is that the dialog doesn't show untill all the async task is done his operations, the dialog is being prompted only in the end. 使用以下代码,将发生对话框,直到所有异步任务完成其操作后才显示该对话框,仅在最后提示该对话框。

Here's the code: 这是代码:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    loadMap();
    progressBar = ProgressDialog.show(Map.this, "", 
            "Loading. Please wait...", true);

    //firing the asynctask here
}

The asynctask is doing pretty heavy operation, it takes like 10 seconds till I'm able to see the layout, that's why I want to prompt the user with a "Loading" dialog. asynctask正在执行非常繁重的操作,大约要花10秒钟才能看到布局,这就是为什么我想用“正在加载”对话框提示用户的原因。 but the user is able to see it only in the end of the asynctask operations for some reason. 但由于某种原因,用户只能在asynctask操作结束时看到它。

What should I do? 我该怎么办?

EDIT: Forgot to mention that in the onProgressUpdate I'm doing some UI operations (Adding the overlays to the mapview). 编辑:忘记提及在onProgressUpdate中,我正在执行一些UI操作(将叠加层添加到mapview)。 That's why the UI is freezing probably. 这就是UI可能冻结的原因。 but I just want to know how to show the loading dialog before, I don't care if the UI freezes after the "loading" dialog shows. 但是我只是想知道之前如何显示加载对话框,我不在乎在显示“加载”对话框之后UI是否冻结。 Actually I do care, but I don't know if there's any better solution. 其实我确实在乎,但我不知道是否有更好的解决方案。

EDIT2: The loadmap function: EDIT2: loadmap函数:

    public void loadMap()
{

        mapView = (TapControlledMapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();
        mapController.animateTo(new GeoPoint((int)(47.975 * 1E6), (int)(17.056 * 1E6)));
        mapView.setOnSingleTapListener(new OnSingleTapListener() {      
            @Override
            public boolean onSingleTap(MotionEvent e) {
                itemizedOverlay.hideAllBalloons();
                return true;
            }
        });
        myLocationOverlay = new FixedMyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocationOverlay);
        mapView.invalidate();
        zoomToMyLocation();

        editText = (EditText)findViewById(R.id.search);
        editText.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus)
                onSearchRequested();
                EditText editText = (EditText)v;
                editText.clearFocus();

            }
        });

}

您是否尝试过将loadMap()移到asynctask中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM