gpt4 book ai didi

java - Android 将值从 onPostExecute 传递到 Activity

转载 作者:行者123 更新时间:2023-12-01 12:40:44 25 4
gpt4 key购买 nike

我是 Java 和 Android 新手,正在为 Google map 开发 Android 应用程序。

我想从Asynctask外部获取从onPostExecute到 Activity 的纬度和经度值。我尝试过 do 方法,但结果没有任何结果。

代码:

@Override
public void onPostExecute(String result) {
super.onPostExecute(result);
List m = new ArrayList<String>();
Log.w("res", result.toString());
StringBuilder sb = new StringBuilder(result);
StringBuilder sb2 = sb.deleteCharAt(sb.indexOf("}"));
System.out.println("after delete }" + sb2.toString());
StringBuilder sb3 = sb2.deleteCharAt(sb2.indexOf("{"));
System.out.println("after delete {" + sb3.toString());

String[] lol = sb3.toString().split(",");
for (int i = 0; i < lol.length; i++) {
System.out.println("lol ??" + lol[i]);
System.out.println("size of lol" + lol.length);
String lol2[] = lol[i].split(":");
System.out.println("size of lol2" + lol2.length);
m.add(lol2[1]);

}
System.out.println("lol2 ??" + m.get(0) + "lon" + m.get(1));
StringBuilder lat_sb = new StringBuilder(m.get(0).toString());
StringBuilder latsbb = lat_sb
.deleteCharAt(lat_sb.indexOf("\""));
StringBuilder lats = latsbb.deleteCharAt(latsbb.indexOf("\""));
System.out.println("lats " + lats);
StringBuilder lon_sb = new StringBuilder(m.get(1).toString());
StringBuilder lonsbb = lon_sb
.deleteCharAt(lon_sb.indexOf("\""));
StringBuilder lons = lonsbb.deleteCharAt(lonsbb.indexOf("\""));
System.out.println("lons " + lons);
double lat = Double.parseDouble(lats.toString().trim());
System.out.println("lat " + lat);
double lon = Double.parseDouble(lons.toString().trim());
System.out.println("lon " + lon);

}

map Activity :

public class Maps extends FragmentActivity  {

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


GoogleMap map =
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview))
.getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(new LatLng(???,???)).title("Marker"));

GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
}
}

请帮我编写代码。

最佳答案

您可以使用嵌套类(将异步任务放入 Activity 中并使用全局变量),也可以使用静态变量。

关于java - Android 将值从 onPostExecute 传递到 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122702/

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