-1

I am trying to pass Latitude and Longitude from one activity to the main activity. In MainActivity, I have defined

public class MainActivity extends AppCompatActivity{
  public double Lat;
  public double Long;

 @Override
  protected void onCreate(Bundle savedInstanceState) {
  ...
    Toast.makeText(this, ""+Lat, Toast.LENGTH_LONG).show();

and using an Intent to transfer the values from another activity (AuxActivity) as:

 adapter.setOnItemClickListener(new CityListAdapter.ClickListener() {
      @Override
      public void onItemClick(View v, int position) {
        City city =adapter.getCityAtPosition(position);
//        Toast.makeText(getApplicationContext(),
//            city.getCity()+"\n"+city.getLatitude()+"\n"+city.getLongitude(),
//            Toast.LENGTH_LONG).show();
        Double Lat = city.getLatitude();
        Double Long = city.getLongitude();
        Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
        mainIntent.putExtra("Lat", Lat);
        mainIntent.putExtra("Long", Long);
        startActivity(mainIntent);
      }

and Lat is always 0.0 in MainActivity, but correct value is given in AuxActivity's Toast.

What I am doing wrong here?

BaRud
  • 3,055
  • 7
  • 41
  • 89

1 Answers1

1

If you notice, you aren't calling the intent values that you are passing in. You would need something like this:

long lat = intent.getLongExtra("LAT");

This would get the intent's long extra being passed into it. Look at this post for further instruction:

How do I get extra data from intent on Android?

beastlyCoder
  • 2,349
  • 4
  • 25
  • 52