2012-03-28 92 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参数。