2016-10-04 66 views
1

我想從AsyncTask類中的全局變量中獲取值時遇到問題。我想從另一個類調用它..如何從(AsyncTask)中獲取列表值,其他類是活動類..?

這裏是我的AsyncTask類代碼:

public class PlacesDisplayTask extends AsyncTask<Object, Integer, List<HashMap<String, String>>> { 

    List<NearbyPlaceModel> data_place = new ArrayList<>(); 
    JSONObject googlePlacesJson; 
    GoogleMap googleMap; 

    @Override 
    protected List<HashMap<String, String>> doInBackground(Object... inputObj) { 

     List<HashMap<String, String>> googlePlacesList = null; 
     Places placeJsonParser = new Places(); 

     try { 
      googleMap = (GoogleMap) inputObj[0]; 
      googlePlacesJson = new JSONObject((String) inputObj[1]); 
      googlePlacesList = placeJsonParser.parse(googlePlacesJson); 
     } catch (Exception e) { 
      Log.d("Exception", e.toString()); 
     } 
     return googlePlacesList; 
    } 

    @Override 
    protected void onPostExecute(List<HashMap<String, String>> list) { 
     googleMap.clear(); 
     data_place.clear(); 
     for (int i = 0; i < list.size(); i++) { 
      if(i < 10) { 
       HashMap<String, String> googlePlace = list.get(i); 
       double lat = Double.parseDouble(googlePlace.get("lat")); 
       double lng = Double.parseDouble(googlePlace.get("lng")); 
       String placeName = googlePlace.get("place_name"); 
       String vicinity = googlePlace.get("vicinity"); 
       LatLng latLng = new LatLng(lat, lng); 
       markerOptions.icon(BitmapDescriptorFactory.fromBitmap(Configure.getBitmapFromURL(googlePlace.get("icon")))); 
       markerOptions.title(placeName + " : " + vicinity); 
       googleMap.addMarker(markerOptions); 

       NearbyPlaceModel items = new NearbyPlaceModel(); 
       items.setVicinity(vicinity); 
       items.setPlace_name(placeName); 
       items.setLat(String.valueOf(lat)); 
       items.setLang(String.valueOf(lng)); 
       data_place.add(items); 
      } 
     } 
    } 

} 

我想從另一個類,這是我的活動,這樣的設置全局變量data_place = new ArrayList<>(); ..

ListView nearby_place = (ListView) findViewById(R.id.itemListView); 
    ListAdapter customAdapter = new ListAdapter(this, R.layout.nearby_place_listview, data_place); 
    nearby_place.setAdapter(customAdapter); 

還是我的代碼有問題..?

+1

[如何獲取OnPostExecute()的結果到主要活動的可能的重複,因爲AsyncTask是一個單獨的類?](http://stackoverflow.com/questions/12575068/how-to-get-the-結果的onpostexecute到主要活動因爲asynctask是a) –

回答

0

您可以通過使用接口得到結果。

  1. 使用所需的回調方法創建接口。
  2. 實現它在你的活動,
  3. 你會得到你的回調方法

在這裏,我給你根據你的代碼的例子的結果。 你MainActivity類別應該像這個 -

public class MainActivity extends AppCompatActivity implements PlaceInterface{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // your code... 

     // calling the AsyncTask and send reference of the interface on it 
     new PlacesDisplayTask(this).execute(); // something like this 

    } 


    @Override 
    public void myPlaceList(ArrayList<NearbyPlaceModel> nearbyPlaceModelArrayList) { 
     ListView nearby_place = (ListView) findViewById(R.id.itemListView); 
     ListAdapter customAdapter = new ListAdapter(this, R.layout.nearby_place_listview, nearbyPlaceModelArrayList); 
     nearby_place.setAdapter(customAdapter); 
    } 
} 

你PlacesDisplayTask類應該像這個 -

public class PlacesDisplayTask extends AsyncTask<Object, Integer, List<HashMap<String, String>>> { 

    ArrayList<NearbyPlaceModel> data_place = new ArrayList<>(); // change 
    JSONObject googlePlacesJson; 
    GoogleMap googleMap; 
    PlaceInterface placeInterface; // change 

    public PlacesDisplayTask(PlaceInterface placeInterface){ // change 
     this.placeInterface = placeInterface; 
    } 

    @Override 
    protected List<HashMap<String, String>> doInBackground(Object... inputObj) { 

     List<HashMap<String, String>> googlePlacesList = null; 
     Places placeJsonParser = new Places(); 

     try { 
      googleMap = (GoogleMap) inputObj[0]; 
      googlePlacesJson = new JSONObject((String) inputObj[1]); 
      googlePlacesList = placeJsonParser.parse(googlePlacesJson); 
     } catch (Exception e) { 
      Log.d("Exception", e.toString()); 
     } 
     return googlePlacesList; 
    } 

    @Override 
    protected void onPostExecute(List<HashMap<String, String>> list) { 
     googleMap.clear(); 
     data_place.clear(); 
     for (int i = 0; i < list.size(); i++) { 
      if(i < 10) { 
       HashMap<String, String> googlePlace = list.get(i); 
       double lat = Double.parseDouble(googlePlace.get("lat")); 
       double lng = Double.parseDouble(googlePlace.get("lng")); 
       String placeName = googlePlace.get("place_name"); 
       String vicinity = googlePlace.get("vicinity"); 
       LatLng latLng = new LatLng(lat, lng); 
      markerOptions.icon(BitmapDescriptorFactory.fromBitmap(Configure.getBitmapFromURL(googlePlace.get("icon")))); 
       markerOptions.title(placeName + " : " + vicinity); 
       googleMap.addMarker(markerOptions); 

       NearbyPlaceModel items = new NearbyPlaceModel(); 
       items.setVicinity(vicinity); 
       items.setPlace_name(placeName); 
       items.setLat(String.valueOf(lat)); 
       items.setLang(String.valueOf(lng)); 
       data_place.add(items); 

       placeInterface.myPlaceList(data_place); // change 
      } 
     } 
    } 

} 

這是界面 -

public interface PlaceInterface { 
    void myPlaceList(ArrayList<NearbyPlaceModel> nearbyPlaceModelArrayList); 
} 

希望這會幫助你。 :)

+0

謝謝你,它的工作原理;) –

0

如果我理解正確!在你的AsyncTask類中創建getter和setter方法。這樣您就可以從您的活動中獲取data_place並設置data_place。

public List<NearbyPlaceModel> getData_place() { 
      return data_place; 
     } 

    public void setData_place(List<NearbyPlaceModel> data_place) { 
      this.data_place = data_place; 
     } 
+0

yeahh我確實創造了這樣的方法,但如何從活動..? –

+0

當你調用AsyncTask的execute方法? –

+0

我從其他asyncTask調用執行代碼,所以我想直接從最後的結果獲得並從活動中獲取,例如:MainActivity-> AsyncTask-> AsyncTask .. –

相關問題