2012-01-14 7 views
1

J'ai une activité qui obtient des données à partir d'Internet, et le montre à l'écran. J'utilise scroll view parce que c'est un texte long, je veux aussi un style de texte différent pour une donnée différente, donc j'utilise peu de textViews avec un style différent et pour le montrer sur l'écran Activity, mon problème est que la vue scroll peut gérer seulement une vue, alors comment puis-je utiliser le défilement pour afficher le style de texte différent, j'ai essayé d'ajouter LinearLayout à scrollView et d'ajouter dynamiquement tous les textViews en code à ce LinearLayout, mais je reçois une exception un enfant direct.Ajouter différents TextViews à ScrollView

Le code ci-dessous:

/** 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); 
    } 

le fichier 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

fournir le code d'ajouter 'TextView' s'il vous plaît – Jin35

+0

scrollLayout = new LinearLayout (this); \t \t \t \t scroll.addView (scrollLayout, LayoutParams.FILL_PARENT, \t \t \t \t \t LayoutParams.WRAP_CONTENT); \t \t pour (WantedData curr: résultat) { \t \t \t if (curr.getTitle() == NULL) { \t \t \t \t break; \t \t \t} \t \t \t title = nouvelle TextView (this); \t \t \t title.setText (curr.getTitle()); \t \t \t scrollLayout.addView (titre, 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 (données, LayoutParams.FILL_PARENT, \t \t \t \t \t LayoutParams.WRAP_CONTENT); – BoazGarty

Répondre

0

Si vous avez défini un LinearLayout en XML vous ne devez pas créer une nouvelle LinearLayout dans votre code, mais vous avez pour récupérer l'existant de cette façon

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

Sinon, vous devez emove le LinearLayout dans votre fichier XML et ajoutez tout par code.

+0

Merci, je sais, mais ce n'est pas le problème im peur, j'ai supprimé la création au code et toujours obtenir exception vue défilement ne peut accueillir qu'un seul enfant direct ..peut-être chaque affichage de texte ajouté à LinearLayout est comme ajouté à la vue de défilement – BoazGarty

+0

@david: c'est très étrange, signaler tout le code de 'onCreate()' méthode - éditer votre question - –

+0

merci, le code édité à ma question. – BoazGarty

1

Le problème est double création de conteneur dans ScrollView. Vous ne devriez pas créer de l'activité, mais prenez déjà défini de xml:

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

Merci, je sais, mais ce n'est pas le problème J'ai peur, j'ai supprimé la création au code et ajouté la référence au fichier XML, toujours obtenir une exception, la vue déroulante peut accueillir un seul enfant direct .. peut-être chaque affichage de texte ajouté le LinearLayout est comme ajouté à la vue déroulante – BoazGarty

Questions connexes