2013-05-11 7 views
5

私のアプリケーションのテキストビューにテキストを設定したいのですが。これは私が使用したコードです:Android TextView setText overlays previous Text

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

実際には動作しますが、以前のテキストは表示されています!たとえば、4は8をオーバーレイします(それ以上テキストを読むことはできません)。

私はサービスを開始するアクティビティを持っています。その後、LocalBroadcastReceiverをアクティビティに登録します。このサービスは、位置オブジェクトをperidicallyアクティビティに送ります。私が得た場所から経度を更新し、新しい経度でテキストビュー(経度のみを含む)を更新したいと考えています。

この問題を解決するためのアイデアがあれば、私は非常に満足しています。

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を投稿できますか? –

+0

はコードで私の質問を更新しました。 – zuluk

答えて

4

これは決して信じられませんが、How often can you update a textview without messにお越しいただいた場合は、この問題に対処していると思われる質問と回答が見つかります。

これで問題が解決しない場合は、テキストを配置するために、を使用して上書きされたonDrawを使用してボタンをカスタム表示に置き換える必要があります。あなただけの非透明の黒色の背景をしたい場合は、あなたのresディレクトリにcolors.xmlファイルを作成(またはに追加)、

...以前の歴史を拭くことを確認することができます:

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

追加:

android:background="@color/black" 

TextViewのxmlに追加します。

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

+0

驚くばかりです、それは同じ問題です。どうもありがとうございました! – zuluk

+0

それは非常に奇妙です!それを聞いていない! この問題を確認する別の方法があります。線形レイアウト内の他のすべてのテキストビューを削除してみてください。それらのうちの1つが他のものの上に重なっているかもしれない。 –

+0

@UdiOshiそれは常にチェックする価値がありますが、私が指している質問ではそうではありません... –

-1

ラインandroid:text=""は冗長であり、あなたはその行を削除することができます。

+1

これは元の質問に対する回答ではありません。それは代わりにコメントとして良いでしょう。 –

関連する問題