1

I have tried work on this for whole day but could find a solution. I am working on a test google map app to learn java. From this activity when button is clicked, it takes user to another activity and I need some values to be passed to next activity. I am able to retrieve Start Address as well as destination address in next activity but not the distance.

implements GeoTask.Geo {
private TextView mFromAddress;
private TextView mToAddress;
String str_from,str_to;
int dist;

public void onClickBtn(View v){
    str_from = mFromAddress.getText().toString();
    str_to = mToAddress.getText().toString();
    String url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=" + str_from + "&destinations=" + str_to + "&mode=driving&language=en-US&avoid=tolls&key=API-Key";
    new GeoTask(MapsActivity.this).execute(url);

    getResults();
}

// This is called from external class GeoTask.Java
public void setDouble(String result) {
    String res[]=result.split(",");
    Double min = Double.parseDouble(res[0])/60;
    dist = Integer.parseInt(res[1])/1000;
}

public void getResults(){
    Intent intent = new Intent(this, ResultsActivity.class);

    String fromCity = str_from;
    String toCity = str_to;
    int kmDistance = dist;

    intent.putExtra("from",fromCity);
    intent.putExtra("to",toCity);
    intent.putExtra("distance",kmDistance);

    startActivity(intent);
}
}

I am retrieving these details by

Intent intent = getIntent();
String fromCity = intent.getStringExtra("from");
String toCity = intent.getStringExtra("to");
int kmDistance = intent.getIntExtra("distance", 0);
Sebastian
  • 286
  • 2
  • 10
  • So `dist` has the correct value before you send it to `ResultsActivity`? It just isn't correct when you try to retrieve it? – Barns Oct 07 '17 at 00:08

1 Answers1

1

Reason why you have no distance.

Maybe the AsyncTask is not yet already done while calling the getResults();

Try to remove the getResults(); after calling the new GeoTask(MapsActivity.this).execute(url); and put the getResults(); in onPostExecute(args)

dotGitignore
  • 1,597
  • 2
  • 15
  • 34