2013-05-02 136 views
0

我創建了一個簡單的應用程序來顯示GPS傳感器衛星信息。 信息顯示爲文本和列表永不停止更新相同的重複信息。 我如何保持獨特的衛星信息?不重複相同的信息。如何避免重複列表中的相同衛星信息?

public void onGpsStatusChanged() { 

    GpsStatus gpsStatus = locationManager.getGpsStatus(null); 
    if(gpsStatus != null) { 
     Iterable<GpsSatellite>satellites = gpsStatus.getSatellites(); 
     Iterator<GpsSatellite>sat = satellites.iterator(); 
     int i=0; 
     while (sat.hasNext()) { 
      GpsSatellite satellite = sat.next(); 
      strGpsStats+= (i++) + ": " + "Pseudo-random number for the satellite: "+satellite.getPrn() + "," + "Satellite was used by the GPS calculation: " + satellite.usedInFix() + "," + "Signal to noise ratio for the satellite: "+satellite.getSnr() + "," + "Azimuth of the satellite in degrees: "+satellite.getAzimuth() + "," +"Elevation of the satellite in degrees: "+satellite.getElevation()+ "\n\n"; 
     } 

     Satinfos.setText(strGpsStats); 
    } 
} 

回答

1

我覺得你的問題是,你不清除每個調用onGpsStatusChanged之間strGpsStats;雖然不確定,因爲你的問題不是很清楚。試試這個:

public void onGpsStatusChanged() { 
    strGpsStats = ""; 
    GpsStatus gpsStatus = locationManager.getGpsStatus(null); 
    if(gpsStatus != null) { 
     Iterable<GpsSatellite>satellites = gpsStatus.getSatellites(); 
     Iterator<GpsSatellite>sat = satellites.iterator(); 
     int i=0; 
     while (sat.hasNext()) { 
      GpsSatellite satellite = sat.next(); 
      strGpsStats+= (i++) + ": " + "Pseudo-random number for the satellite: "+satellite.getPrn() + "," + "Satellite was used by the GPS calculation: " + satellite.usedInFix() + "," + "Signal to noise ratio for the satellite: "+satellite.getSnr() + "," + "Azimuth of the satellite in degrees: "+satellite.getAzimuth() + "," +"Elevation of the satellite in degrees: "+satellite.getElevation()+ "\n\n"; 
     } 

     Satinfos.setText(strGpsStats); 
    } 
} 
0

進入while後,需要檢查重複信息。

while (sat.hasNext()) { 
      GpsSatellite satellite = sat.next(); 
      if(sat.Current != sat.next) 
{ 
      strGpsStats+= (i++) + ": " + "Pseudo-random number for the satellite: "+satellite.getPrn() + "," + "Satellite was used by the GPS calculation: " + satellite.usedInFix() + "," + "Signal to noise ratio for the satellite: "+satellite.getSnr() + "," + "Azimuth of the satellite in degrees: "+satellite.getAzimuth() + "," +"Elevation of the satellite in degrees: "+satellite.getElevation()+ "\n\n"; 
     } 
} 
+0

也許需要數組? if data(i ++)!= data(i) – user2342687 2013-05-02 10:50:44

+0

sat.Current not exist! – user2342687 2013-05-02 10:52:00