3

我有一个程序可以搜索一些元素并在 GoogleMap 上显示这些元素。我想在所有在地图上设置各种地理点的程序结束之前显示一个进度对话框。

搜索我发现此代码在方法结束之前显示进度对话框。

    ProgressDialog dialog; 
private class Test extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
        dialog = new ProgressDialog(Main.this); 
        dialog.setMessage("Loading...."); 
        dialog.setIndeterminate(true); 
        dialog.setCancelable(true); 
        dialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... voids) { 
        try { 
            runOnUiThread(new Runnable() { 
                public void run() { 

                } 
            }); 
            //your code 

    } 

    @Override 
    protected void onPostExecute(Void params) { 
        dialog.dismiss(); 
        //result 

    } 
} 

我必须添加异步任务的类是这样的:

public class FindItOnMap extends MapActivity{
static String[] foundResults;

@Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.ricerca_condominio);
         mapView = (MapView)findViewById(R.id.mapView);

         mapController = mapView.getController();

         mapView.setClickable(true);

         mapView.setBuiltInZoomControls(true);

         mapController.setZoom(12);

         myLocationOverlay = new MyLocationOverlay(this, mapView);
         List<Overlay> overlays = mapView.getOverlays();
         overlays.add(myLocationOverlay);
         myLocationOverlay.enableMyLocation();
                 ...........



         ((ImageButton) findViewById(R.id.btSearch)).setOnClickListener(mSearchListenerListener);


}


        OnClickListener mSearchListener = new OnClickListener() {
            public void onClick(View v) {
                    String Location=editorLocation.getText().toString();
                    String name=editorName.getText().toString();
                //static method that updates the lists foundResult
                search(name, location);
                //static method that use the static array foundResult to update the map
                updateMapWithResult();

            }
     };

当我点击一个按钮时,在返回解决方案之前需要时间的方法被调用

search(name, location);

updateMapWithResult();

问题是我的类扩展了 MapActivity,我不能使用多重继承来扩展另一个类。我该怎么做才能解决问题?

4

3 回答 3

2

所以算法不应该是棘手的。

因此,将您的 Class 实现为AsyncTask Activity Main Class的内部类。然后我建议您在执行之前调用方法:show()Task

progressDialog.show();
Test test = new Test();
test.execute();

注意:你不应该在方法中调用show()方法onPreExecuteAsyncTask

然后在你的方法onPostExecute只需调用dismiss()解雇 ProgressDialog

@Override 
protected void onPostExecute(Void params) { 
   progressDialog.dismiss(); 
} 

现在它应该可以工作了。


@Override 
    protected Void doInBackground(Void... voids) { 
        try { 
            runOnUiThread(new Runnable() { 
                public void run() { 

                } 
            }); 
            //your code 

    } 

永远不要再这样做了!doInBackground方法是为后台线程上的长任务设计的方法,在这种方法中你不应该,你不能,你不能更新你的!UI

为了更新您的UI AsyncTask报价方法,onProgressUpdate并且onPostExecute您应该尊重它。

于 2012-07-07T12:01:33.143 回答
1

如果您不想使用多重继承,您可以将您定义AsyncTask为内部类并使用AsycTask您定义的实例化对象来完成您的工作。像这样的东西:

public class FindItOnMap extends MapActivity {

static String[] foundResults;
private ProgressDialog dialog;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.ricerca_condominio);
     mapView = (MapView)findViewById(R.id.mapView);

     mapController = mapView.getController();

     mapView.setClickable(true);

     mapView.setBuiltInZoomControls(true);

     mapController.setZoom(12);

     myLocationOverlay = new MyLocationOverlay(this, mapView);
     List<Overlay> overlays = mapView.getOverlays();
     overlays.add(myLocationOverlay);
     myLocationOverlay.enableMyLocation();
             ...........



     ((ImageButton) findViewById(R.id.btSearch)).setOnClickListener(mSearchListenerListener);


     }

    OnClickListener mSearchListener = new OnClickListener() {
        public void onClick(View v) {
                String Location=editorLocation.getText().toString();
                String name=editorName.getText().toString();
             //Call the AsyncTask here
            new YourCustomAsyncTask().execute(new String[] {name, location});

        }


    private class YourCustomAsyncTask extends AsyncTask <String, Void, Void> { 

        @Override 
        protected void onPreExecute() { 
           dialog = new ProgressDialog(Main.this); 
           dialog.setMessage("Loading...."); 
           dialog.setIndeterminate(true); 
           dialog.setCancelable(true); 
           dialog.show(); //Maybe you should call it in ruinOnUIThread in doInBackGround as suggested from a previous answer
        } 

        @Override 
        protected Void doInBackground(String... strings) { 
           try { 

              search(strings[0], string[1]);

              runOnUiThread(new Runnable() { 
                 public void run() { 
                    updateMapWithResult(); //Or call it onPostExecute before progressDialog's dismiss. I believe this method updates the UI so it should run on UI thread
                 } 
               }); 

           } catch(Exception e) {
           }

        }

    @Override 
    protected void onPostExecute(Void params) { 
        dialog.dismiss(); 
        //result 

    } 


.....
}
于 2012-07-07T11:24:33.093 回答
0
    public class FindItOnMap extends MapActivity{
static String[] foundResults;
 String Location;
 String name;
@Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.ricerca_condominio);
         mapView = (MapView)findViewById(R.id.mapView);
         mapController = mapView.getController();
         mapView.setClickable(true);
         mapView.setBuiltInZoomControls(true);
         mapController.setZoom(12);
         myLocationOverlay = new MyLocationOverlay(this, mapView);
         List<Overlay> overlays = mapView.getOverlays();
         overlays.add(myLocationOverlay);
         myLocationOverlay.enableMyLocation();
                 ...........



        ((ImageButton)findViewById(R.id.btSearch))  
                      .setOnClickListener(mSearchListenerListener);

        OnClickListener mSearchListener = new OnClickListener() {
            public void onClick(View v) 
         {
              Location=editorLocation.getText().toString();
              name=editorName.getText().toString();

    /*===You Just Go On to Call search(name,location) in the doInBackground part  
 and updateMapWithResult(); in the postExecute() part so that   
only after gettting the result from the search method,the updateMapWithResult();   
will be called in the Async Task like this...==*/

              new Test().execute();

        }
     };
}

    private class Test extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() 
    { 
       dialog=ProgressDialog.show(YourClassContext,"Loading","",false);

    } 

    @Override 
    protected Void doInBackground(Void... voids)
    { 
    try 
    { 

      search(name, location);

    } 
    }); 


    } 

    @Override 
    protected void onPostExecute(Void params) 
    { 
       updateMapWithResult();
       dialog.dismiss(); 
    //result 

    } 
    } 
于 2012-07-07T11:37:22.403 回答