2013-05-11 71 views
5

我想將文本設置爲我的應用程序的文本視圖(提示)。這是我使用的代碼:Android TextView setText覆蓋以前的文本

TextView lonTextView = (TextView) findViewById(R.id.textViewLonWert); 
lonTextView.setText(longitude.toString()); 

其實它工作,但前面的文本仍然顯示!例如,一個4覆蓋8(你不能再閱讀文本)。

我有一個活動啓動服務。然後註冊LocalBroadcastReceiver到活動。該服務將定位對象定期發送到活動。從我得到的位置經度,並想更新一個textview(只包含經度)與新的經度。

如果有人有解決這個問題的想法,我會非常高興。

UPDATE: 完整的代碼是很長,所以這裏是相關片段:

TrackingActivity.java

public class TrackingActivity extends Activity { 

// GPSTracker class 
GPSTracker gps; 

public String filename; 

/* Um eigene Position anzuzeigen */ 
ArrayList<OverlayItem> overlayItemArray; 
protected MapView mapView; 
protected MapController mapController; 
public Boolean isPause; 
public Boolean isTracking; 
public MyLocationOverlay myLocationOverlay; 
PathOverlay myPath; 
List<GeoPoint> path; 

public BroadcastReceiver screenReceiver; 

/* Textviews setzen */ 
TextView lonTextView; 
TextView latTextView; 
TextView altTextView; 
TextView accTextView; 

// Initiating Menu XML file (menu.xml) 
/*@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 

    MenuInflater menuInflater = getMenuInflater(); 
    menuInflater.inflate(R.menu.menu_tracking, menu); 
    MenuItem mi_startTracking = menu.findItem(R.id.start_tracking); 
    mi_startTracking.setVisible(true); 
    return true; 
}*/ 

/** 
* Event Handling for Individual menu item selected 
* Identify single menu item by it's id 
* */ 
@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    ... 
}  

@Override 

public boolean onPrepareOptionsMenu(Menu menu) { 
    ... 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tracking); 

    isTracking = false; 
    isPause  = false; 

    // create GPS-class object 
    gps = new GPSTracker(TrackingActivity.this 
         ,null); 

    myPath = new PathOverlay(Color.RED, this); 
    path = new ArrayList<GeoPoint>(); 

    /* Map anzeigen */ 
    mapView = new MapView(this, 500); 
    mapView.setTileSource(TileSourceFactory.MAPNIK); 
    org.osmdroid.views.MapView.LayoutParams mapParams = new     org.osmdroid.views.MapView.LayoutParams(
      org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT, 
      org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT, 
      null, 0, 0, 0); 
    /*löst Fehler aus: mapView.setClickable(true);*/ 
    mapView.setBuiltInZoomControls(true); 
    mapController = new MapController(mapView); 
    mapView.getController().setZoom(15); 


    //--- Create Overlay 
    overlayItemArray = new ArrayList<OverlayItem>(); 

    DefaultResourceProxyImpl defaultResourceProxyImpl = new DefaultResourceProxyImpl(this); 
    MyItemizedIconOverlay myItemizedIconOverlay   = new MyItemizedIconOverlay(
                 overlayItemArray, null, defaultResourceProxyImpl); 
    mapView.getOverlays().add(myItemizedIconOverlay); 
    //--- 

    //Add current position 
    myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 

    /* Lege Pfad auf Karte */ 
    mapView.getOverlays().add(myPath); 
    mapView.invalidate(); 
    LinearLayout myLayout = (LinearLayout) findViewById(R.id.trackingLinearLayoutMap); 

    myLayout.addView(mapView,mapParams); 

} 

private BroadcastReceiver gpsDataReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     //Log.d("TrackingActivity", "in Receiver"); 
     /* alles was der Service liefert */ 
     String action  = intent.getAction(); 
     Double altitude  = intent.getDoubleExtra("altitude", 0); 
     Double longitude = intent.getDoubleExtra("longitude", 0); 
     Double latitude  = intent.getDoubleExtra("latitude", 0); 
     Bundle b   = intent.getExtras(); 
     Location location = (Location)b.get("location"); 

     Log.d("received","Receiver: "+longitude+" ; "+latitude); 


     //Log.d("TrackingActivity", "vor GeoPoint"); 
     GeoPoint locGeoPoint = new GeoPoint(latitude, longitude); 

       /* Erstelle Pfad */ 
     if(isTracking){ 
      path.add(locGeoPoint); 
      myPath.addPoint(locGeoPoint); 
     } 
     //Log.d("TrackingActivity", "nach GeoPoint: "+locGeoPoint.toString()); 
     mapController.setCenter(locGeoPoint); 

     setOverlayLoc(location); 

     /* Textviews Werte setzen */ 
     lonTextView = (TextView) findViewById(R.id.textViewLonWert); 
     lonTextView.setText(longitude.toString()); 
     //lonTextView.invalidate(); 

     latTextView = (TextView) findViewById(R.id.textViewLatWert); 
     latTextView.setText(latitude.toString()); 
     //latTextView.invalidate(); 

     altTextView = (TextView) findViewById(R.id.textViewAltWert); 
     altTextView.setText(altitude.toString()); 
     //altTextView.invalidate(); 

     accTextView = (TextView) findViewById(R.id.textViewAccWert); 
     accTextView.setText(Float.toString(location.getAccuracy())+" m"); 
     //accTextView.invalidate(); 

     /* Karte aktualisieren */ 
     mapView.invalidate(); 

    } 
}; 
... 
@Override 
protected void onResume() { 
    super.onResume(); 
    /* Registriere BroadcastReceiver für GPSDaten */ 
    LocalBroadcastManager.getInstance(this).registerReceiver(gpsDataReceiver, new IntentFilter("gpsdata")); 

     ... 

} 
... 

這裏我佈局的.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/trackingLinearLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/trackingStatusOuterWrapper" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="kein GPS Signal" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 

    <LinearLayout 
    android:id="@+id/trackingStautsInnerWrapper2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
     <TextView 
     android:id="@+id/textViewLonLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Länge: " 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 
     <TextView 
     android:id="@+id/textViewLonWert" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 
     <TextView 
     android:id="@+id/textViewLatLabel" 
     android:layout_marginLeft="40dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Breite: " 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 
     <TextView 
     android:id="@+id/textViewLatWert" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 

    </LinearLayout> 
    <LinearLayout 
    android:id="@+id/trackingStautsInnerWrapper3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

     <TextView 
     android:id="@+id/textViewAltLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Höhe: " 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 
     <TextView 
     android:id="@+id/textViewAltWert" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 
     <TextView 
     android:id="@+id/textViewAccLabel" 
     android:layout_marginLeft="40dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Genauigkeit: " 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 
     <TextView 
     android:id="@+id/textViewAccWert" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textSize="10sp" /> 
    </LinearLayout> 
</LinearLayout> 
<!-- 
<org.osmdroid.views.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mapview" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:clickable="true"/> 
--> 

<LinearLayout 
    android:id="@+id/trackingLinearLayoutMap" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

</LinearLayout> 

希望它有幫助!

+2

你可以發佈完整的代碼和xml嗎?r – 2013-05-11 14:18:38

+0

用代碼更新了我的問題。 – zuluk 2013-05-11 14:40:01

回答

4

你永遠不會相信這一點,但如果你轉到How often can you update a textview without mess你會發現一個問題和答案,我認爲這是解決這個問題。

如果這些問題無法解決問題,則可能需要使用自定義視圖替換該按鈕,並使用canvas.drawText()覆蓋onDraw來將文本置於原位。然後,您可一定要消滅任何以前的歷史...

如果你只是想要一個非透明的黑色背景,創建(或增加)的colors.xml文件在您res目錄:

<resources> 
    <color name="black">#ff000000</color> 
</resources> 

並添加:

android:background="@color/black" 

對於TextView的xml。

+0

太棒了,這是同樣的問題。非常感謝你! – zuluk 2013-05-11 14:46:05

+0

這很奇怪!沒聽說過! 還有另一種方法來檢查這個問題。嘗試刪除線性佈局內的所有其他文本視圖。也許其中一個重疊在另一個上。 – 2013-05-11 14:46:08

+0

@UdiOshi這是總是值得檢查,但不是在我指出的問題的情況... – 2013-05-11 15:12:39

-1

android:text=""

<TextView 
    android:id="@+id/textViewLonWert" 
    android:layout_width="80dp" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:textSize="10sp" /> 

是多餘的,你可以刪除了這一行。

+1

這不是對原始問題的回答。相反,它會更好。 – 2016-03-14 21:12:14