LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
SharedPreferences settings = getSharedPreferences("cars4sale",0);
SharedPreferences.Editor GPS = settings.edit();
GPS.putFloat("latitude", (float)location.getLatitude());
GPS.putFloat("longitude", (float)location.getLongitude());
GPS.commit();
TextView GPSTV = (TextView)findViewById(R.id.GPSTV);
SharedPreferences coords = getSharedPreferences("cars4sale",0);
String gpsstr = GPS.getFloat("gps", "");
GPSTV.setText(gpsstr);
The main problem is eclipse wants me to change getFloat to putFloat and then when I 开发者_如何学Godo that it wants me to do getInt and then goes in a loop between float and Int. I just want to store a number to a textview field!
Because coords is a sharedpreferences
, and I don't think you want to setText there?
Don't you mean
GPSTV.setText(gpsstr);
This:
String gpsstr = GPS.getFloat("gps", "");
Doesn't work, because GPS
is your Editor, not your sharedpreferences
. You need this
String gpsstr = String.valueOf(coords.getFloat("gps", 0));
GPSTV.setText(gpsstr);
精彩评论