0

I have an application that gets your current location. It will return your current address, latitude and longitude. When you click convert it takes you to a new class.

I want to get latitude and longitude (if it has something) into a textView in another class.

FIRST CLASS

public class MainActivity extends AppCompatActivity {

private static Button newWindowBtn; //new window button

Button btnShowLocation;
GPSTracker gps;
Double latitude;
Double longitude;
String addressStr;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            shareIt();
        }
    });


    final TextView address = (TextView)findViewById(R.id.textView);

    onClickButtonListener(); //NEW WINDOW CLICK LISTENER

    btnShowLocation = (Button) findViewById(R.id.show_location); //GPS BUTTON

    final Geocoder geocoder = new Geocoder(this,Locale.ENGLISH);

    btnShowLocation.setOnClickListener(new View.OnClickListener()
    { //GPS CLICK LISTENER
        @Override
        public void onClick(View v) { //SHOWS LOCATION
            gps = new GPSTracker(MainActivity.this);

            if(gps.canGetLocation()){
                latitude =  gps.getLatitude();
                longitude = gps.getLongitude();


                try{
                    List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
                    if(addresses != null)
                    {
                        Address returnedAddress = addresses.get(0);
                        StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                        for (int i = 0; i < returnedAddress.getMaxAddressLineIndex();i++){
                            strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                        }


                        address.setText(strReturnedAddress.toString() + "Latitude: " + latitude + "\nLongitude: " + longitude);

                        addressStr = strReturnedAddress.toString();

                    }
                    else
                    {
                        address.setText("No address returned");
                    }
                }
                catch(IOException e){
                    e.printStackTrace();
                    address.setText("Can't get address");
                }
            }else{
                gps.showSettingsAlert();
            }
        }
    });
}

SECOND CLASS

public class secondActivity extends AppCompatActivity {

    Button convertBtn;
    TextView latitudeTxt;
    TextView longitudeTxt;
    TextView latitudeDMS;
    TextView longitudeDMS;



    convertTo convert = new convertTo();

    MainActivity mainActivity = new MainActivity();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareIt();
            }
        });

        convertBtn = (Button) findViewById(R.id.convertBtn);

        latitudeTxt = (TextView)findViewById(R.id.latitudeTxt);
        longitudeTxt = (TextView)findViewById(R.id.longitudeTxt);

        latitudeDMS = (TextView)findViewById(R.id.latitudeDMS);
        longitudeDMS = (TextView)findViewById(R.id.longitudeDMS);



        convertBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

So I would want latitudeTxt to have the latitude from the previous screen (first class). I'm a student and trying to learn.

I've tried adding this to the first class:

secondActivity secondActivity = new secondActivity();
secondActivity.latitudeTxt.setText(latitude);

This didn't work.

I tried in the second class

MainActivity mainActivity= new MainActivity();
latitudeTxt.setText(mainActivity.latitude);
nubteens
  • 5,462
  • 4
  • 20
  • 31
Marcella Ruiz
  • 323
  • 1
  • 3
  • 15
  • You can do this one: http://stackoverflow.com/questions/14292398/how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android – Hiren Patel Dec 04 '15 at 07:17

3 Answers3

0

This approach is totally wrong and discouraged by Android.

MainActivity mainActivity= new MainActivity();

never EVER do that.

What you in fact are looking for is sharing data between activities, (there are a lot of questions like this in the community) and this is how this works

in the fisrt class (actually activity) use this as ref.:

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

and in the second:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}
nubteens
  • 5,462
  • 4
  • 20
  • 31
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Send the latitude ,longitude values from 1st class to 2nd using bundle extras. Extract the value in 2nd class and set the values to the respective text view.

Rec
  • 5
  • 3
0

You should not create Objects of Activities like this,

MainActivity mainActivity= new MainActivity();

Activities are started by using Intents and if you need to refer to current object of your activity then you can do it by MainActivity.this.

And as far as, "This didn't work." is concerned, that is because you are creating a new object of your Activity class (which is correct syntax wise). And the new object will have its own copy of instance variables. So it will not be having values of already existing Activity object (which is shown to you).

To pass your values from say, FirstActivity to SecondActivity, do something like,

FirstActivity

Intent secondActivity = new Intent(FirstActivity.this, SecondActivity.class);
secondActivity.putExtra("latitude", value);
secondActivity.putExtra("longitude", value);
startActivity(secondActivity);

Then in SecondActivity

Intent intent = getIntent();
String latitude = intent.getStringExtra("latitude");
String longitude = intent.getStringExtra("longitude");
gprathour
  • 14,813
  • 5
  • 66
  • 90