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!

2
  • I think I've found my own answer. Global Variables. [Global Variables in Java][1] [1]: stackoverflow.com/questions/4646577/global-variables-in-java Commented Nov 3, 2014 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) Commented Nov 3, 2014 at 12:29

2 Answers 2

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

Comments

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.

1 Comment

Using statics for this is just bad design. I would totally not recommend it.

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.