2012-03-28 91 views
0

我已經創建了一個XML頁面,其中包含2個文本視圖和一個沒有ID的seekbar。 CustomSeekBar類使用xml頁面作爲基本結構創建這些對象。 您可以在我的模擬器上看到textviews的空間,但我很難搞清楚如何設置文本。很明顯,我錯過了一些東西,因爲CustomSeekBar類沒有辦法告訴我要爲哪些文本視圖設置文本。沒有ID的Android TextView setText

如何在不給每個textview設置硬編碼ID的情況下設置每個單獨視圖的文本? 我之所以說沒有硬編碼ID,是因爲如果每個文本視圖都被命名,那麼當一個文本視圖的文本需要更改時,是不是所有的文本視圖文本都會改變? 由於我的customseekbar類與活動有複合關係,我該如何調用特定的textview ID?

調用一切的活動。

public class ColorsActivity extends ListActivity { 
/** Called when the activity is first created. */ 

//Array Adapter that will hold our ArrayList and display the items on the ListView 
SeekBarAdaptor seekBarAdaptor; 

//List that will host our items and allow us to modify that array adapter 
ArrayList<CustomSeekBar> seekBarArrayList=null; 
// TextView myValueText; 

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

    //Initialize ListView   
    ListView lstTest= getListView(); 

    //Initialize our ArrayList 
    seekBarArrayList = new ArrayList<CustomSeekBar>(); 

    //Initialize our array adapter 
    seekBarAdaptor = new SeekBarAdaptor(ColorsActivity.this, R.layout.seekbars, seekBarArrayList); 

    CustomSeekBar red = new CustomSeekBar(this, "red", 1); 
    //CustomSeekBar blue = new CustomSeekBar(this, "blue"); 
    //CustomSeekBar green = new CustomSeekBar(this, "green"); 

    //Set the above adapter as the adapter of choice for our list 
    lstTest.setAdapter(seekBarAdaptor); 

    seekBarArrayList.add(red); 
    //seekBarArrayList.add(blue); 
    //seekBarArrayList.add(green); 

    Amarino.connect(this, "00:11:11:21:05:53"); 
} 







} 

CustomSeekBar類

public class CustomSeekBar implements SeekBar.OnSeekBarChangeListener { 

Context myContext; 
TextView myValue; 
TextView myLabel; 
SeekBar mySeekBar; 

CustomSeekBar(Context context, String label, int ID){ 
    myContext = context; 


    myValue = new TextView(myContext); 
    mySeekBar = new SeekBar(myContext); 

    myValue.setText(label); 
    mySeekBar.setOnSeekBarChangeListener(this); 

} 

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { 
    myValue.setText(progress); 

    Amarino.sendDataToArduino(myContext, "00:11:11:21:05:53", 'A', progress); 
} 
public void onStopTrackingTouch(SeekBar seekBar){ 

} 
public void onStartTrackingTouch (SeekBar seekBar){ 
} 


} 

seekbarlist.xml握住我的自定義列表列表視圖

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </ListView> 

</LinearLayout> 

seekbars.xml是每個自定義列表項的結構(CustomSeekBar)

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/seekBarLayout"> 
    <TextView 
     android:gravity="center_horizontal" 
     android:background="#aa0000" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     /> 

     <TextView 
     android:gravity="center_horizontal" 
     android:background="#aa0000" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     /> 


    <SeekBar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:max="255"/> 

</LinearLayout> 
+5

如果你沒有名字,我不認識你,我怎麼會在人羣中打電話給你,給你答案? – 2012-03-28 02:50:53

+0

我給出了一個簡短的解釋和跟進問題。 – Spectrem 2012-03-28 14:24:10

回答

0

w hy你會不得不爲他們命名相同的ID名稱爲什麼不是 @ + id/textview1和@ + id/textview2然後引用你的代碼中的兩個文本框我不明白什麼阻止了你?

+0

我不會將它們命名爲相同的ID。每個CustomSeekBar對象有2個textview(在seekbars.xml中)。這些ID可能不同。但是當我有多個CustomSeekBar對象時會發生什麼?將會有4,6,8等文字瀏覽。其中一半將具有相同的ID。 – Spectrem 2012-03-28 20:52:50

0

您可以使用ID作爲@ bmporter12說。您可以擁有重複的ID,前提是Android可以從您告訴findViewById開始查找。因此,在您的適配器中,在getView()中,您將從seekbars.xml中將新的row充氣,然後執行row.findViewById(R.id.textView1)row.findViewById(R.id.textView2)

如果您需要從適配器外部設置它,那麼根據您獲取設置TextView的信號的位置,您的CustomSeekBar可能會要求Activity在適配器的特定位置處輸入它,或者它可以使用在onClick回調中傳遞的View參數。