2016-04-27 13 views
0

私の最初のAsyncTask doInBackgroundメソッドでは、Google Place APIから場所のリストを取得するメソッドを実行します。この最初のAsyncTaskのpostExecuteの中で、これらの場所のすべての名前を取得し、それらをすべてListViewに表示します。別のAsynctaskのonPostExecute内でAsyncTaskを実行し、結果を返します

私は今現在の場所から1つの場所の運転距離を表示したいと思います(私はすでにそれを得ることができます)。そうするために、別のクラスで別のAsyncTaskを作成してこの距離を取得しました。ここでは、コードは次のようになります。

public class Distance extends AsyncTask<Double, Double, String> { 
    GooglePlaces googlePlaces; 
    String distancePlace = null; 
    @Override 
    final protected String doInBackground(Double... params) { 
     double lat1,lat2,lon1,lon2; 
     lat1=params[0]; 
     lon1=params[1]; 
     lat2=params[2]; 
     lon2=params[3]; 
     googlePlaces = new GooglePlaces(); 
     distancePlace= googlePlaces.getDistance(lat1,lon1,lat2,lon2); 
     return distancePlace; 
    } 
} 

、これが私の最初のAsyncTask postExecuteのコードです:

protected void onPostExecute(String s) { 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       //get json status 
       String status = nearPlaces.status; 
       if (status.equals("OK")){ 
        if (nearPlaces.results != null){ 
         //every single place 
         for (Place p : nearPlaces.results){ 
         //just a try, here I would like to get the distance 
         /* 
          Double[] myparams = {gps.getLatitude(),gps.getLongitude(), 
            p.geometry.location.lat,p.geometry.location.lng}; 
          new Distance().execute(myparams);*/ 

          HashMap<String,String> map = new HashMap<String, String>(); 
           map.put(KEY_NAME,p.name); 
           //add hashmap 
           placesListItems.add(map); 
          } 
         ListAdapter adapter = new SimpleAdapter(GpsActivity.this, placesListItems, R.layout.list_item, new String[] {KEY_REFERENCE,KEY_NAME}, 
           new int[] {R.id.reference, R.id.name}); 
         //add into listview 
         lv.setAdapter(adapter); 
        } 

私の問題は私のpostExecute内部の「距離AsyncTask」を実行し、私にその結果を返す方法です最初のAsyncTask、私のListViewに表示する。

答えて

0

あなたはこのような何かを行うことができます。

Distance distance = new Distance(){ 
    protected void onPostExecute(final String result1) { 
     // First AsyncTask result. 
     Distance distance2 = new Distance(){ 
      protected void onPostExecute(String result2) { 
       // process the second result. 
       // Because the first result "result1" is "final", 
       // it can be accessed inside this method. 
      } 
     }; 
     distance2.execute(...); 
    } 
}; 
distance.execute(...); 

はまた、あなたがonPostExecute(...)メソッドはUIスレッド上で実行されるためrunOnUiThread(...)を使用する必要はありません。

+0

「for」の直前で、コードをどこに置く必要があるのか​​分かりません。そして 'onPostExecute(String result2)'の残りの部分( 'HashMap'など)の中に入れますか? – user6262006

+0

@ user6262006私の答えは、別のものが終了したときに 'AsyncTask'を開始する方法と、2番目のメソッドの' onPostExecute(...) 'メソッドの中で最初のタスクの結果にアクセスする方法を示しています。あなたが達成しようとしていることがわからないので、これをどのように使うことができるかについて、より具体的にすることはできません。 – Titus

+0

私が投稿したコードに見られるように、 'nearPlaces.result'のリストのすべての場所について、同じアクティビティではなく、別のクラスにある' Distance AsyncTask'を実行する必要があります。結果はすべての場所の 'Distance AsyncTask'の結果であるので、' HashMap'にそれを加えて、それぞれの場所の名前で表示することができます。私のコードで見ることができる 'postExecute'メソッドは、場所を検索する他のAsyncTaskからのものです。 – user6262006

関連する問題