0

我有一个问题要解决,我有三个文本视图数组。

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

我需要在表格中显示它们,如下所示:因为我需要显示它。但是当用以下代码显示时,屏幕是这样的:所以不需要像这样sho

   ((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);

    }    
}
4

1 回答 1

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>

于 2012-08-30T15:11:04.630 回答