Android:为什么这个代码不允许我设置文本(浮动?)?

时间:2011-06-08 20:24:23

标签: android floating-point textview sharedpreferences

    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);

主要问题是eclipse要我将getFloat更改为putFloat然后当我这样做时它要求我做getInt然后进入float和Int之间的循环。我只想将一个数字存储到textview字段中!

1 个答案:

答案 0 :(得分:3)

因为coords是sharedpreferences,我认为你不想在那里设置文本?

你不是说

GPSTV.setText(gpsstr);

此:

String gpsstr = GPS.getFloat("gps", "");    

不起作用,因为GPS是您的编辑,而不是您的sharedpreferences。你需要这个

String gpsstr = String.valueOf(coords.getFloat("gps", 0));    
GPSTV.setText(gpsstr);