gpt4 book ai didi

java - 如何将类返回到 onPostExecute?

转载 作者:行者123 更新时间:2023-11-29 21:34:45 28 4
gpt4 key购买 nike

更新,请见下文。

如何将我的类 LocationData 和我的 ArrayList listOfObjects 返回到 onPostExecute()?我想在我的 UI 中使用它,现在它在 AsyncTask 的后台。我还想添加标记:

mMap.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.title(name));

这样我就可以在每次循环后将每个新位置添加到 map 中。

我是否在返回 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"));

这看起来很简单,但我已经创建了这个方法: 公共(public) ArrayList getLocationData() {

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

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

return listOfObjects;
}

在我的 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"));

这是我根据@Sustain 的建议所做的更改。我现在似乎没有得到任何 map 标记。有人看到了什么吗?

public class FinderActivity extends Activity implements LocationListener  {


GoogleMap mMap;
Location myLocation;
EditText length;
String lengthString;
LocationManager locationmanager;
double lati;
double longi;
String nameFirst1;
//Spinner s;

List<Address> address;
Geocoder coder = new Geocoder(this);
private static final String TAG_ID = "id";
private static final String TAG_FIRSTNAME = "nameFirst";
private static final String TAG_LASTNAME = "nameLast";
private static final String TAG_EMAIL = "emailAddress";
private static final String TAG_ADDRESS = "streetAddress";
private static final String TAG_STATE = "state";

private static final String TAG_PHONE = "phone";
JSONArray contacts = null;


private static 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;

}

}

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap!= null) {

mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);


mMap.setMyLocationEnabled(true);
mMap.animateCamera(CameraUpdateFactory.zoomBy(17));

}

LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria cr = new Criteria();
String provider = locationmanager.getBestProvider(cr, true);

Location location = locationmanager.getLastKnownLocation(provider);

locationmanager.requestLocationUpdates(provider, 20, 0, (LocationListener) this);

mMap.moveCamera(CameraUpdateFactory.newLatLng((new LatLng(location.getLatitude(), location.getLongitude()))));


new EndpointsTask().execute(getApplicationContext());

}

public class EndpointsTask extends AsyncTask<Context, LocationData, Long> {

private List<LocationData> locationList = new ArrayList<LocationData>();

public Long doInBackground(Context... contexts) {

Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();

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);

LocationData data = new LocationData(lati, longi, nameFirst1);
locationList.add(data);
publishProgress(data);

}

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

}

protected void onProgressUpdate(LocationData data) {
// Add Marker on Map using data. This is called by
// publishProgress(LocationData) on the UI Thread.
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.title(nameFirst1));

Log.d("bananas", lati + longi + nameFirst1);
}

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

}

}

最佳答案

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

class LocationData {
// final Fields
// Constructor
// Getters
}

或者如果您不在其他任何地方使用它,则作为最外层类的私有(private)静态类。

然后对于你的 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;
}
}

这样,您可以在获取下一个标记之前在 map 上单独发布每个标记。

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

关于java - 如何将类返回到 onPostExecute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18647614/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com