2012-07-31 59 views
0

嗨即時嘗試設置一個文本視圖的值與.setText在由線程運行的方法,但它被附加到TextView代替。參數字符串f是一個正在發送的方法,在for循環是這樣的:TextView setText附加代替

for (int j = 0; j < 5; j++) { 
     String[] string = array[j].split(":"); 
     String y= string[0]; 
     String x= string[1]; 
     determineSeat(y, something, x); 
    } 
} 

private void determineSeat(String is, String cs, String f) 
{ 
      TextView txtHand = null; 
    String txt = null; 

    if (iSeat < cSeat) { 
     x = cSeat - iSeat; 

     if (x == 1) { 
      txtHand = (TextView) findViewById(R.id.txtHand8); 
      txt = txtHand.getText().toString(); 
      txtHand.setText(""); 
      txtHand.setText(txt + ":" + f); 
     } 
} 

XML:

<TextView 
    android:id="@+id/txtHand1" 
    android:layout_width="73dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:text=" " /> 

我希望它是每次清除我把東西TextView的,而不是被附加的任何人都知道問題是什麼?

回答

2

你將它設置爲"txt + ":" + f""txt"被定義爲任何已經在textview中。如果您只是希望每次調用方法時都將其設置爲"f",請執行"txtHand.setText(f);"因爲您要求它取出當前文本視圖中的任何內容並添加冒號"f."

+0

在Yosem發佈後開始撰寫,所以我沒有看到他的帖子。基本上,他說什麼。 – gmaster 2012-07-31 21:54:41

+0

在我看來,你的回答更加清晰,更好地描述瞭如何解決問題。你有我的投票。 – 2012-07-31 21:56:20

+0

Thx,對於答案,我實際上看到了什麼問題現在它更多,我忘記了我要做的txt字符串。很久很忙,我不知道我做錯了什麼,對不起。此主題已被回答 – Vern 2012-07-31 22:38:35

1

我認爲你是分配這個無功電流的文字:

txt = txtHand.getText().toString(); 

然後將其與該行前面加上到:f

txtHand.setText(txt + ":" + f); 
0

我想這是因爲你是初始化一個新的文本框對象evrytime內循環,然後將其內容添加到新的。您應該在循環外聲明並初始化文本框。

TextView txtHand = (TextView) findViewById(R.id.txtHand8); 

for (int j = 0; j < 5; j++) { 
     String[] string = array[j].split(":"); 
     String y= string[0]; 
     String x= string[1]; 
     determineSeat(y, something, x); 
    } 
} 

private void determineSeat(String is, String cs, String f) 
{ 
    String txt = null; 

    if (iSeat < cSeat) { 
     x = cSeat - iSeat; 

     if (x == 1) { 
      txtHand.setText(":" + f); 
     } 
} 

,什麼是字符串和CS的