简体   繁体   English

如何在Android studio java的textview中显示另一个类的变量?

[英]How can I show a variable from another class in a textview in Android studio java?

So I have a GPS tracking app and I want to show my data (distance, speed etc) in one of the UI.所以我有一个 GPS 跟踪应用程序,我想在其中一个 UI 中显示我的数据(距离、速度等)。

My LocationService.java tracks all the metrics in a foreground service:我的 LocationService.java 跟踪前台服务中的所有指标:

public class LocationService extends Service implements LocationListener{

public ArrayList<Location> lastLocationResultArray = new ArrayList<>();
public float distance;
public float distanceKm;
public float ascent;
public float descent;
public float currentSpeed;
Location changedLocation;
double currentAltitude;
public float averageSpeed;

private static DecimalFormat df1 = new DecimalFormat("#");
private static DecimalFormat df2 = new DecimalFormat("#.#");
private static DecimalFormat df3 = new DecimalFormat("#.##");

public static final String EXTRA_DISTANCE = "com.example.iathleticsrecorder.EXTRA_DISTANCE";

public LocationCallback locationCallback = new LocationCallback(){
    @Override
    public void onLocationResult(LocationResult locationResult) {
        super.onLocationResult(locationResult);
        if(locationResult != null && locationResult.getLastLocation() != null){
            Location actualLocation = locationResult.getLastLocation();
            double latitude = locationResult.getLastLocation().getLatitude();
            double longitude = locationResult.getLastLocation().getLongitude();
            double altitude = locationResult.getLastLocation().getAltitude();
            double speed = locationResult.getLastLocation().getSpeed();
            Log.d("LOCATION_UPDATE", latitude + ", " + longitude + ", " + altitude + ", " + locationResult);

            currentSpeed = (float) (speed*3.6f);
            
            changedLocation = actualLocation;

            if(!lastLocationResultArray.isEmpty()) {
               distance += changedLocation.distanceTo(lastLocationResultArray.get(lastLocationResultArray.size() - 1));
                distanceKm = distance/1000;

                Location lastlocation = lastLocationResultArray.get(lastLocationResultArray.size() - 1);
                double lastAltitude = lastlocation.getAltitude();
                double altitudeChange = altitude-lastAltitude;
                if(altitudeChange<-9) {
                    descent += altitudeChange;
                } else if(altitudeChange>9) {
                    ascent += altitudeChange;
                }

            }

            lastLocationResultArray.add(changedLocation);
            Log.d("ENDOFLOOP", distanceKm + ", " + distance + ", " + ascent + ", " + descent + ", " + currentSpeed + " ," + averageSpeed );
        }
    }
};

public String getDistance() {
    return df3.format(distance);
}

Here is an example of how all the metrics are added up from the "Log.d (ENDOFLOOP)":以下是如何从“Log.d (ENDOFLOOP)”中添加所有指标的示例:

2020-10-05 19:32:55.486 20116-20116/com.example.iathleticsrecorder D/ENDOFLOOP: 0.0042962608858942986, 4.296261, 4.296261, 22.800003, -22.800003, 0.08298418 ,0.0 2020-10-05 19:32:55.486 20116-20116/com.example.iathleticsrecorder D/ENDOFLOOP:0.0042962608858942986, 4.296261, 4.296261, 4.2962.80,20.2080,20.80,20.80

So I know that the variables declared at the top of the LocationService-class is changing (as seen in the log.d), but when I try to get the data from the other class, it will only show "0" in the textview's, as if it does not take the updated value from LocationService.class, but only the declared value (even though I have not decared any value, only the variable).所以我知道在 LocationService 类顶部声明的变量正在发生变化(如 log.d 中所示),但是当我尝试从另一个类获取数据时,它只会在 textview 中显示“0” ,好像它没有从 LocationService.class 中获取更新的值,而只是声明的值(即使我没有 decared 任何值,只有变量)。 Here's a snippet of the code that is connected to the UI in the StartedSession.java:下面是连接到 StartedSession.java 中的 UI 的代码片段:

I declare the LocationService-class:我声明 LocationService 类:

LocationService mLocationService = new LocationService();

And use a getter to get the value of the float variable via a button:并使用 getter 通过按钮获取浮点变量的值:

        btUpdateNumbers = findViewById(R.id.btUpdateNumbers);
    btUpdateNumbers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtViewDistance.setText(mLocationService.getDistance());
            //txtViewCurrentSpeed.setText(mLocationService.getSpeed());
        }
    });

Why is it only showing "0"?为什么只显示“0”?

使用 SharedPreferences 可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM