Java 如何将类返回到onPostExecute?

Java 如何将类返回到onPostExecute?,java,android,arrays,google-maps,Java,Android,Arrays,Google Maps,更新,请参阅下面 如何将类LocationData和ArrayList ListofObject返回到onPostExecute()?我想在我的UI中使用它,现在它在异步任务的后台。此外,我还想添加带有以下内容的标记: mMap.addMarker(new MarkerOptions() .position(new LatLng(lati, longi)) .title(name)); 这样我就可以在每次循环后将每个新位置添加到地图中 在返回LocationData类后,是否将

更新,请参阅下面

如何将类LocationData和ArrayList ListofObject返回到onPostExecute()?我想在我的UI中使用它,现在它在异步任务的后台。此外,我还想添加带有以下内容的标记:

mMap.addMarker(new MarkerOptions()
    .position(new LatLng(lati, longi))
    .title(name));
这样我就可以在每次循环后将每个新位置添加到地图中

在返回LocationData类后,是否将上述内容放在onPostExecute中

    try {

        String apples = endpoint.listContactInfo().execute().toString();

        JSONObject jObject = new JSONObject(apples);

        JSONArray jsonArr = jObject.getJSONArray("items");

        for (int i = 0; i < jsonArr.length(); i++) {
            JSONObject jsonObj1 = jsonArr.getJSONObject(i);

            // Storing each json item in variable
            String id = jsonObj1.getString(TAG_ID);
            String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
            String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
            String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
            String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
            String phone1 = jsonObj1.getString(TAG_PHONE);

            // test to see if made it to string
            Log.d("YOUR_TAG", "First Name: " + nameFirst1 + " Last Name: "
                    + nameLast1);

            address = coder.getFromLocationName(streetAddress1, 5);

            Address location1 = address.get(0);

            // SET LAT LNG VALUES FOR MARKER POINT

            lati = location1.getLatitude();
            longi = location1.getLongitude();

            Log.d("Location", "Location:" + lati + " " + longi);

            class LocationData {
                private double lat;
                private double longitude;
                private String name;

                public LocationData(double lat, double longitude,
                        String name) {
                    this.lat = lat;
                    this.longitude = longitude;
                    this.name = name;
                }

                public void setLat(double lat) {
                    this.lat = lat;
                }

                public void setLongitude(double longitude) {
                    this.longitude = longitude;
                }

                public double getLat() {
                    return lat;
                }

                public double getLongitude() {
                    return longitude;
                }

                public void setName(String name) {
                    this.name = name;
                }

                public String getName() {
                    return name;
                }

            }

            ArrayList<LocationData> listOfObjects = new ArrayList<LocationData>();

            listOfObjects.add(new LocationData(lati, longi, nameFirst1));

        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return (long) 0;

}

// WHAT DO I PUT HERE TO RETURN LocationData Class here
// ADD MARKER TO MAP UI
protected void onPostExecute() {

    // mMap.addMarker(new MarkerOptions()
    // .position(new LatLng(lati, longi))
    // .title("Hello world"));
试试看{
字符串apples=endpoint.listContactInfo().execute().toString();
JSONObject jObject=新的JSONObject(苹果);
JSONArray jsonArr=jObject.getJSONArray(“项目”);
for(int i=0;i
这似乎很简单,但我创建了以下方法: 公共阵列列表getLocationData(){

ArrayList listOfObjects=new ArrayList();
添加(新位置数据(lati、longi、nameFirst1));
返回对象列表;
}
在我的LocationData类中。然后,我将LocationData.getLocationData()与onPostExecute一起放置,然后我得到无法解析的LocationData。此时代码看起来如下所示:

 try {

    String apples = endpoint.listContactInfo().execute().toString();

    JSONObject jObject = new JSONObject(apples);

    JSONArray jsonArr = jObject.getJSONArray("items");

     for(int i =0 ; i<jsonArr.length() ;i++ ){
         JSONObject jsonObj1 = jsonArr.getJSONObject(i);


                    // Storing each json item in variable
                    String id = jsonObj1.getString(TAG_ID);
                    final String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
                    String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
                    String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
                    String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
                    String phone1 = jsonObj1.getString(TAG_PHONE);

                    //test to see if made it to string
                    Log.d("YOUR_TAG", "First Name: " + nameFirst1 + " Last Name: " + nameLast1);

                       address = coder.getFromLocationName(streetAddress1,5);

                        Address location1 = address.get(0);

                        // SET LAT LNG VALUES FOR MARKER POINT

                     lati = location1.getLatitude();
                         longi = location1.getLongitude();

                         Log.d("Location", "Location:" + lati + " " +  longi);


                       class LocationData {
                             private double lat;
                             private double longitude;
                             private String name;

                             public LocationData(double lat, double longitude, String name) {
                                 this.lat = lat;
                                 this.longitude = longitude;
                                 this.name = name;
                             }

                             public void setLat(double lat) {
                                 this.lat = lat;
                             }

                             public void setLongitude(double longitude) {
                                 this.longitude = longitude;
                             }

                             public double getLat() {
                                 return lat;
                             }

                             public double getLongitude() {
                                 return longitude;
                             }

                             public void setName(String name) {
                                   this.name = name;
                                }

                             public String getName() {
                                 return name;



                                }

                             public ArrayList<LocationData> getLocationData() {

                                 ArrayList<LocationData> listOfObjects = new ArrayList<LocationData>();

                                 listOfObjects.add(new LocationData(lati, longi, nameFirst1));

                                 return listOfObjects;
                             }

                             }

     }

    } catch (IOException e) {
    e.printStackTrace();
  } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
      return (long) 0;


    }
    //WHAT DO I PUT HERE TO RETURN LocationData Class here
         // ADD MARKER TO MAP UI
    protected void onPostExecute(Long result ) {

                  //CANT BE RESOLVED
        LocationData.getLocationData();

        //mMap.addMarker(new MarkerOptions()
        //.position(new LatLng(lati, longi))
        // .title("Hello world"));
试试看{
字符串apples=endpoint.listContactInfo().execute().toString();
JSONObject jObject=新的JSONObject(苹果);
JSONArray jsonArr=jObject.getJSONArray(“项目”);

for(inti=0;ionPostExecute)在UI线程中运行。因此,用户体验中的任何更改都可以在这里完成,在您的例子中,可以向映射添加标记

onPostExecute接受从doInBackground()返回的结果参数

您可以从中了解有关AsyncTask的更多信息


它也有一个很好的例子。

类LocationData是在一个不可访问的范围内定义的。相反,在它自己的.java文件中定义它,如下所示:

class LocationData {
    // final Fields
    // Constructor
    // Getters
}
或者作为最外层类的私有静态类,如果您不在其他任何地方使用它

然后,对于AsyncTask的子类,可以有如下内容:

private class AsyncJsonTask extends AsyncTask<Param, LocationData, Void>
{
    private List<LocationData> locationList = new ArrayList<LocationData>();
    // ...
    protected void doInBackground(Param) {
        // ...
        for (int i = 0; i < jsonArr.length(); i++) {
            // Do your stuff with JSon Objects
            // ...
            // Instanciate a new LocationData and pass it as progress:
            LocationData data = new LocationData(latitude, longitude, name);
            locationList.add(data);
            publishProgress(data);
        }
    }

    protected void onProgressUpdate(LocationData data) {
        // Add Marker on Map using data. This is called by
        // publishProgress(LocationData) on the UI Thread.
        mMap.addMarker(/* marker */);
    }

    protected void onPostExecute() {
        // Assign outer class member field the value of the builded list
        // for future reference.
        mLocationList = locationList;
    }
}
私有类AsyncJsonTask扩展了AsyncTask
{
private List locationList=new ArrayList();
// ...
受保护的void doInBackground(参数){
// ...
for(int i=0;i
这样,在获取下一个标记之前,可以在地图上单独发布每个标记


作为旁注,您应该研究静态方法和字段的含义;您对
LocationData.getLocationData()
的调用将无效。

我似乎无法在onPostExecute中解析我的LocationData类引用。谢谢。我已将代码如上所述放置。但是我没有得到任何映射标记
class LocationData {
    // final Fields
    // Constructor
    // Getters
}
private class AsyncJsonTask extends AsyncTask<Param, LocationData, Void>
{
    private List<LocationData> locationList = new ArrayList<LocationData>();
    // ...
    protected void doInBackground(Param) {
        // ...
        for (int i = 0; i < jsonArr.length(); i++) {
            // Do your stuff with JSon Objects
            // ...
            // Instanciate a new LocationData and pass it as progress:
            LocationData data = new LocationData(latitude, longitude, name);
            locationList.add(data);
            publishProgress(data);
        }
    }

    protected void onProgressUpdate(LocationData data) {
        // Add Marker on Map using data. This is called by
        // publishProgress(LocationData) on the UI Thread.
        mMap.addMarker(/* marker */);
    }

    protected void onPostExecute() {
        // Assign outer class member field the value of the builded list
        // for future reference.
        mLocationList = locationList;
    }
}