2012-01-14 2 views
1

Iインターネットからデータを取得し、画面に表示するアクティビティがあります。 私はスクロールビューを使用しているので、長いテキストで、別のデータ用に別のテキストスタイルを使用したいので、別のスタイルのtextViewsを使用せず、アクティビティ画面に表示します。 私の問題は、 1つのビューしかないので、どのようにスクロールして別のスタイルのテキストビューを表示できますか、私はscrollViewにLinearLayoutを追加し、このLinearLayoutにコード内のすべてのtextViewを動的に追加しようとしましたが、 1人の直接の子供。異なるテキストビューをScrollViewに追加する

以下のコード:

/** this is the function, which called from the onClick method. 
wanted data object contains 2 strings title message and the message itself. 

When debug the code i can see that there's two String values in each loop. 
but i cant add the linearLayout to my scrollView - exception ScrollView can host only one direct child */ 

    private void showResult(ArrayList<WantedData> result) { 
     // TODO Auto-generated method stub 
     TextView title; 
     TextView data; 
     scrollLayout = (LinearLayout) findViewById(R.id.LlScrollView); 
     for (WantedData curr : result) { 
      if (curr.getTitle() == null) { 
       break; 
      } 

      title = new TextView(this); 
      title.setText(curr.getTitle()); 

      scrollLayout.addView(title, LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      data = new TextView(this); 
      data.setText(curr.getData()); 
      scrollLayout.addView(data, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
     } 
     scroll.addView(scrollLayout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 

     //at the onCreate method - scroll = (ScrollView) findViewById(R.id.SvShowTextFromServer); 
    } 

xmlファイル:

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

    <include 
     android:id="@+id/layout_reffernce" 
     layout="@layout/explore" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Enter City" /> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <EditText 
       android:id="@+id/EtCity" 
       android:layout_width="210dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.14" 
       android:orientation="vertical" > 

       <requestFocus /> 
      </EditText> 

      <Button 
       android:id="@+id/bSearchCity" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Search" /> 
     </LinearLayout> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Enter State" /> 

     <EditText 
      android:id="@+id/EtState" 
      android:layout_width="253dp" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" /> 
    </LinearLayout> 

    <ScrollView 
     android:id="@+id/SvShowTextFromServer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <LinearLayout 
      android:id="@+id/LlScrollView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/backround" 
      android:orientation="vertical" > 
     </LinearLayout> 


    </ScrollView> 

</LinearLayout> 
+0

は 'TextView'ください – Jin35

+0

scrollLayoutを=新しいのLinearLayout(this)を追加するコードを提供します。 \t \t \t \t scroll.addView(scrollLayoutを、LayoutParams.FILL_PARENT、 \t \t \t \t \t LayoutParams.WRAP_CONTENT)。 (WantedDataのCURR:結果)のため \t \t { \t \t \t IF(curr.getTitle()== NULL){ \t \t \t \tブレーク。 \t \t \t} =新規のTextView(本) \t \t \t表題。 \t \t \t title.setText(curr.getTitle()); \t \t \t scrollLayout.addView(タイトル、LayoutParams.FILL_PARENT、 \t \t \t \t \t LayoutParams.WRAP_CONTENT)。 \t \t \t data = new TextView(this); \t \t \t data.setText(curr.getData()); \t \t \t \t scrollLayout.addView(データ、LayoutParams.FILL_PARENT、 \t \t \t \t \t LayoutParams.WRAP_CONTENT)。 – BoazGarty

答えて

0

あなたがあなたのコード内で新しいLinearLayoutを作成する必要はありませんが、あなたが持っているXMLでLinearLayoutを定義した場合このように既存のものを取得するには

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LlScrollView); 

そうでなければ、rあなたのXMLにLinearLayoutをemoveし、すべてのコードを追加してください。

+0

ありがとう、私は知っているが、私はコードで作成を削除して、まだ例外スクロールビューを取得することができますが、私は恐怖の問題ではない知っているただ1つの直接の子をホストすることができます..おそらく、LinearLayoutに追加された各テキストビューは、スクロールビュー – BoazGarty

+0

に追加されたようなものです@david: 'onCreate()'メソッドのすべてのコードを報告してください - –

+0

ありがとうございます。 – BoazGarty

1

問題は、ScrollViewのコンテナの二重作成です。あなたは活動でそれを作成するべきではありませんが、すでにXMLから定義取る:

LinearLayout scrollContainer = (LinearLayout)findViewById(R.id.LlScrollView); 
for (...) { 
    //create here some text 
    scrollLayout.addView(text); 
} 
+0

ありがとう、私は知っているが、私はコードで作成を削除し、xmlファイルへの参照を追加しても、例外をスクロールビューは1つの直接の子をホストすることができます..おそらく各テキストビューを追加LinearLayoutはスクロールビューに追加されているようです – BoazGarty

関連する問題