2012-03-28 12 views
0

2つのテキストビューとIDなしのシークバーを保持するxmlページを作成しました。 クラスCustomSeekBarは、xmlページを基本構造として使用してこれらのオブジェクトを作成します。 私のエミュレータでtextviewsの領域を見ることができますが、テキストを設定するのに苦労しています。明らかに、私は何かが欠けている。なぜなら、CustomSeekBarクラスがどのテキストビューをテキストを設定したいのかを知る方法がないからです。IDなしのTextView setText

各テキストビューにハードコードされたIDを付けずに、個々のビューのテキストを設定するにはどうすればよいですか? ハードコードされたIDなしで言う理由は、各テキストビューに名前が付けられていれば、1つのテキストビューのテキストを変更する必要があるため、そのIDを持つテキストビューのすべてのテキストが変更されないからです。 私のcustomseekbarクラスがアクティビティとの複合関係にあるので、どのように特定のテキストビュー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

あなたは名前がなく、私はあなたを知らない場合、あなたに答えを与えるために群衆の中であなたをどのように呼びますか? –

+0

私は質問にフォローアップの簡単な説明を与えました。 – Spectrem

答えて

0

wなぜあなたはそれらのID名を同じ名前にしなければならないのですか @ + id/textview1と@ + id/textview2そしてあなたのコードで2つのテキストボックスを参照しています。

+0

私はそれらに同じIDを付けません。 CustomSeekBarオブジェクトごとに2つのテキストビュー(seekbars.xml内)があります。これらのIDは異なる可能性があります。しかし、複数のCustomSeekBarオブジェクトがあるとどうなりますか? 4,6,8などのテキストビューがあります。それらの半数は同じIDを持ちます。 – Spectrem

0

IDは@ bmporter12と同じように使用できます。あなたはそれがfindViewByIdにそれを言うときに見て開始する場所を持っていれば、あなたは重複IDを持つことができます。したがって、アダプタではgetView()に、rowをseekbars.xmlから膨張させてから、row.findViewById(R.id.textView1)row.findViewById(R.id.textView2)を実行します。

アダプタの外側から設定する必要がある場合は、信号がTextViewを設定する場所に応じて、CustomSeekBarがアクティビティにアダプタの特定の位置での入力を要求するか、 onClickコールバックで渡されたViewパラメータを使用します。

関連する問題