2012-08-30 48 views
0

我有一個問題要解決,我有三個textviews數組。如何在一個tableLayout中顯示textview數組

- Textview a[]; 
- TexTview b[]; 
- TexTview c[]: 

我需要在表格中顯示它們,就像這樣:as I need to show it。 但與下面的代碼顯示,畫面是這樣的:so don't need sho like this

((ViewGroup) layout).addView(a[i]); 
    ((ViewGroup)layout).addView(a[i]); 
    ((ViewGroup)layout).addView(a[i]); 

這是完整的代碼,提前,非常感謝你。

public class MainActivity extends Activity { 

    SitesList sitesList = null; 

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

     /** Create a new layout to display the view */ 
     View layout = findViewById(R.id.layout); 

     /** Create a new textview array to display the results */ 

     TextView variacion[]; 
     TextView nemotecnico[]; 
     TextView precio[]; 

     try { 

      /** Handling XML */ 
      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser sp = spf.newSAXParser(); 
      XMLReader xr = sp.getXMLReader(); 

      /** Send URL to parse XML Tags */ 
      URL sourceUrl = new URL("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=75"); 

      /** Create handler to handle XML Tags (extends DefaultHandler) */ 
      MyXMLHandler myXMLHandler = new MyXMLHandler(); 
      xr.setContentHandler(myXMLHandler); 
      xr.parse(new InputSource(sourceUrl.openStream())); 

      } catch (Exception e) { 
       System.out.println("XML Pasing Excpetion = " + e); 
      } 

     /** Get result from MyXMLHandler SitlesList Object */ 
     sitesList = MyXMLHandler.sitesList; 

     /** Assign textview array lenght by arraylist size */ 
    // registro = new TextView[sitesList.getRegistro().size()]; 
     nemotecnico = new TextView[sitesList.getNemotecnico().size()]; 
     variacion = new TextView[sitesList.getVariacion().size()]; 
     precio = new TextView[sitesList.getPrecio().size()]; 


     /** Set the result text in textview and add it to layout */ 
     for (int i = 0; i < sitesList.getRegistro().size(); i++) { 

      variacion[i] = new TextView(this); 
      variacion[i].setText(sitesList.getVariacion().get(i)); 

      nemotecnico[i] = new TextView(this); 
      nemotecnico[i].setText(sitesList.getNemotecnico().get(i)); 

      precio[i] = new TextView(this); 
      precio[i].setText(sitesList.getPrecio().get(i)); 

      ((ViewGroup)layout).addView(nemotecnico[i]); 
      ((ViewGroup)layout).addView(variacion[i]); 
      ((ViewGroup)layout).addView(precio[i]); 

     } 
     /** Set the layout view to display */ 
     setContentView(layout); 

    }  
} 
+0

多解釋 –

回答

0

我會做一個ListView出它與具有頭部和在GridLayout其他4個TextViews每個自定義行中的自定義行。然後,您可以編寫自定義適配器,以便在重寫getView()方法時填充它們。

這是一個很好的演示:http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/

您的自定義行會是這個樣子:

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

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="TextView1" /> 

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:columnCount="2" 
    android:rowCount="2" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView2" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView3" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView4" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView5" /> 

</GridLayout> 

相關問題