0

I'm not great with android so I'm just going to ask you this:

I want to transfer some "data" (GPS Coordinates) from my location listener to a string, that will be used in an onClick function. Such as:

case R.id.sendButton:

        ParsePush push = new ParsePush();
        String message = "Hey, My coordinates are - LONG:" + loc.getLongitude();;

        push.setChannel("test1");
        push.setMessage(message);
        push.sendInBackground();


        break;

Yes, I DO have a location listener:

   class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
            mlocation.setText("");

            Toast.makeText(
                    getBaseContext(),
                    "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                        + loc.getLongitude(), Toast.LENGTH_SHORT).show();
            String longitude = "Longitude: " + loc.getLongitude();
            Log.v("Long", longitude);
            String latitude = "Latitude: " + loc.getLatitude();
            Log.v("Lat", latitude); 

ETC....

So basically, I want to be able to set my longitude to a certain variable(string) and use that string in my onClick button.

How can I do this? Any links whatsoever would be great. Thanks!

Deepzz
  • 4,573
  • 1
  • 28
  • 52
Paramone
  • 2,634
  • 4
  • 31
  • 58
  • I think I've found my own answer. Global Variables. [Global Variables in Java][1] [1]: http://stackoverflow.com/questions/4646577/global-variables-in-java – Paramone Nov 03 '14 at 12:16
  • 1
    please be very careful with this! If your application is inactive for a while, it can be, that Android will delete the static contents in order to free up RAM. If your activity gets active again, those fields are null! (Before accessing them, check if they got nulled and handle that accordingly) – Michael Nov 03 '14 at 12:29

2 Answers2

1

Don't use global variables (static) variables!!! Bad very very bad! You should only use those in some very select programming issues.

Use a get pattern for problems like this! The example code below shows how you can use the get (and set) pattern.

class MyLocationListener implements LocationListener {

    private String longitude;
    private String latitude;


    public String getLongitude(){
        return longitude;
    }

    public String getLatitude(){
        return latitude;
    }

    @Override
    public void onLocationChanged(Location loc) {
        longitude = "Longitude: " + loc.getLongitude();
        Log.v("Long", longitude);
        atitude = "Latitude: " + loc.getLatitude();
        Log.v("Lat", latitude); 
    }

}

Keep an instance of your listener around in your Activity

//Initialize your listener in the onCreate for example
MyLocationListener listener = ;

To get the longitude or latitude you would use:

//In the onClick
if(listener.getLongitude() != null){
    //Do something with the value.
} else {
    //No longitude available yet.
}
Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
0

Declare the variable you want to use outside of the class or want to access it througthout the class as global like,

public static String mystring;

If you want to access it in some other class,access it via the classname.mystring. If you want to access it in same class just use by accessing mystring.

micky
  • 508
  • 5
  • 17