如何在tableLayout中显示textview数组

时间:2012-08-30 14:47:01

标签: java android xml arrays textview

我有一个问题需要解决,我有三组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);

    }    
}

1 个答案:

答案 0 :(得分:0)

我会使用自定义行创建一个ListView,其中每个自定义行的GridLayout中都包含标题和其他4个TextView。然后,当您覆盖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>