gpt4 book ai didi

android - 如何在 Java (Android Studio) 中的类之间传递变量值?

转载 作者:行者123 更新时间:2023-11-29 00:07:29 25 4
gpt4 key购买 nike

这是我的变量:

public class List extends Activity {

double lat;
double lon;

public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
lat = loc.getLatitude();
lon = loc.getLongitude();
}
.......
}

我想在这里获取纬度和经度值:

public class AmbilData extends AsyncTask<String, String, String> {
.....

protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub

String url;

url = "http://anjayanjay.esy.es?lat="+lat+"&lon="+lon; //here i need the variable value
.......................
}

我刚开始使用 android studio,当我运行这段代码时,lat = 0 和 lon = 0。谁能告诉我如何解决这个问题?

最佳答案

您可以通过构造函数传递它:

public class List extends Activity {

double lat;
double lon;

public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
lat = loc.getLatitude();
lon = loc.getLongitude();
new AmbilData(loc).execute();
}
.......
}

public class AmbilData extends AsyncTask<String, String, String> {
private Location mLoc;
.....

public AmbilData(Location loc) {
mLoc = loc;
}

protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub

String url;

url = "http://anjayanjay.esy.es?lat="+mLoc.getLatitude()+"&lon="+mLoc.getLongitude(); //here i need the variable value
.......................
}

或者通过AsyncTaskParam,这需要改变你的AsyncTask的第一个泛型:

public class List extends Activity {

double lat;
double lon;

public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
lat = loc.getLatitude();
lon = loc.getLongitude();
new AmbilData().execute(loc);
}
.......
}

public class AmbilData extends AsyncTask<Location, String, String> {
.....

protected String doInBackground(Location... arg0) {
// TODO Auto-generated method stub

String url;

url = "http://anjayanjay.esy.es?lat="+arg0[0].getLatitude()+"&lon="+arg0[0].getLongitude(); //here i need the variable value
.......................
}

关于android - 如何在 Java (Android Studio) 中的类之间传递变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32814567/

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