2

This is my variable:

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();
  }
.......
}

I want to get the lat and lon values here:

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
 .......................
 }

I'm new in using android studio, this code has lat = 0, and lon = 0 when I ran it. Could someone tell me how to fix this?

1
  • dont know what's your problem and where you call your AsysncTask. Commented Sep 28, 2015 at 2:10

2 Answers 2

2

You can pass it via constructor:

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
 .......................
 }

or through the Param of AsyncTask, this need change your AsyncTask's first generic type:

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
 .......................
 }
Sign up to request clarification or add additional context in comments.

Comments

0

As I see, you get data from an AsynTask. Override onPostExcute() in your AsyncTask. Data (String) in doInbackground() available here when done. For simple example:

 @Override
protected void onPostExecute(String string) {
    // Call back Data and Display the current string data in the UI
    activity.callBackDataFromAsyncTask(string);
}

Hope this help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.