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

したがって、アルゴリズムはトリッキーであってはなりません。

したがって、クラスをアクティビティメインクラスの内部クラスAsyncTask として実装します。次に、実行する前にメソッドを呼び出すことをお勧めします。show()Task

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

注:show()メソッドonPreExecuteのメソッドを呼び出さないでくださいAsyncTask

次に、あなたの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 に答える