2

I am getting my values in a random variable in a string here as cityname:-

 public static String cityName;
LocationManager locationManager = (LocationManager)
        getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();

private class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        String longitude = "Longitude: " + loc.getLongitude();
        Log.v(TAG, longitude);
        String latitude = "Latitude: " + loc.getLatitude();
        Log.v(TAG, latitude);
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses;
        try {
            addresses = gcd.getFromLocation(loc.getLatitude(),
                    loc.getLongitude(), 1);
            if (addresses.size() > 0) {
                System.out.println(addresses.get(0).getLocality());
                cityName = addresses.get(0).getLocality();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

I want to get the value coming to cityname as a string resource in xml,is it possible in some way?

2
  • Why you want to store cityname in Android string resource? Commented Nov 25, 2016 at 15:00
  • I want to use it as a default value in shared preferences,in shared preferences you need an integer resource value.. Commented Nov 25, 2016 at 15:21

1 Answer 1

1

I guess you want to use cityname somewhere else in the app thats why you want to store it in string res file. So if you want to use the cityname somewhere else try to store it in sharedpreferences or in a file to use it later. Becoz its not possible to store strings dynamically in strings.xml file

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putString("defaultCityName", cityName).apply();

And to use this

public static String getPreferredLocation(Context context) { 
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
   String defaultCityName = prefs.getString("defaultCityName","testCity");
   return prefs.getString(context.gettring(R.string.pref_location_key‌​}, defaultCityName); 
}
Sign up to request clarification or add additional context in comments.

8 Comments

For which method of sharedpreference?
In this method i want to replace R.String.PREF_LOCATION_DEFAULTS with my string of cityname. public static String getPreferredLocation(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); return prefs.getString(context.getString(R.string.pref_location_key), context.getString(R.string.pref_location_defaults)); }
you can send a string variable as a parameter as a default variable to prefs.getString() method. You can send your cityName variable as a default value but to access this cityName every where you need to store it somewhere. Make a prefs string for default cityName and store this cityName and send this as default value.
Can you explain to me how?I still can't figure out how can i use cityname which is a variable inside the prefs.getString as a default.How do i make pref string that uses the cityname variable?
As soon as you get cityName store it in sharedpreference like
|

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.