没有在表格中看到任何数据

时间:2014-08-06 12:28:47

标签: android xml android-tablelayout

我在查看我正在尝试填充TableLayout的数据时遇到问题。当我运行代码时它没有错误输出但是表格没有填满数据它只是显示出来空白,我能看到的就是我在xml file中定义的标题行。

我已经查看了多个示例,并且已经弄清楚了那里给出的一些答案,比如在我的行周围切换参数(我还没有找到正确的组合)并且我已经改变了我从viewwidget但仍然没有......

public void createTable(String xmlForTable) 
    throws XmlPullParserException, IOException {
//public void createTable(){

    Integer i = 0;
    endPlacementTag = "nothing";
    //setContentView(R.layout.activity_all_tires);

     lo = (TableLayout)  findViewById(R.id.tiresTable);

     TableRow headerRow = new TableRow(this);
     TableRow.LayoutParams hp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
     headerRow.setLayoutParams(hp);

     tv = new TextView(this);
     tv.setText("Text Header");
     headerRow.addView(tv);
     lo.addView(headerRow);

     XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
     factory.setNamespaceAware(true);
     XmlPullParser xpp = factory.newPullParser();

     xpp.setInput(new StringReader (xmlForTable));
     int eventType = xpp.getEventType();

     TableRow row= new TableRow(this);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(lp);

        rfid = new TextView(this);
        projectNumber = new TextView(this);
        inspectionLotNumber = new TextView(this);
        serialization = new TextView(this);
        spec = new TextView(this);
        sku = new TextView(this);
        materialNumber = new TextView(this);


     while (eventType != XmlPullParser.END_DOCUMENT) {

        if(eventType == XmlPullParser.START_DOCUMENT) {

              System.out.println("Start document");

          } else if(eventType == XmlPullParser.END_DOCUMENT) {

              System.out.println("End document");

          } else if(eventType == XmlPullParser.START_TAG) {

              System.out.println("Start tag "+xpp.getName());
              placementTag = xpp.getName();

          } else if(eventType == XmlPullParser.END_TAG) {

              System.out.println("End tag "+xpp.getName());
              endPlacementTag = "End tag "+xpp.getName();

          } else if(eventType == XmlPullParser.TEXT) {

              System.out.println("Text "+xpp.getText());
          }

           ....//filled with more if statements same as this one just different .equals

          if(placementTag.equals("MaterialNumber"))
              {
                  materialNumber.setText(xpp.getText());
                  materialNumber.setTextSize(10);                   
                  materialNumber.setLayoutParams(new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                  //row.addView(materialNumber);
                  placementTag = "";

              }
              else if(endPlacementTag.equals("End tag Table")) 
              {

                  row.addView(rfid);
                  row.addView(projectNumber);
                  row.addView(inspectionLotNumber);
                  row.addView(serialization);
                  row.addView(spec);
                  row.addView(sku);
                  row.addView(materialNumber);

                  row.setGravity(Gravity.CENTER);
                  row.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                  lo.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                  i++;
                  row.removeAllViews();
                  lo.removeAllViews();

              }

              }
          eventType = xpp.next();
          }

这是我的XML:

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0C0C0"    >

<!-- Once the column headers are known go back into the string.xml file and add them to elimiate warnings -->

<TableRow android:background="#C0C0C0" 

    >

    <TextView
        android:layout_width="200dp"
        android:layout_column="0"
        android:layout_weight="1"
        android:text="NUM" />

    <TextView
        android:layout_width="200dp"
        android:layout_column="1"
        android:layout_weight="1"
        android:text="Project Number" />

    <TextView
        android:layout_width="200dp"
        android:layout_column="2"
        android:layout_weight="1"
        android:text="Inspection Lot Number" />

    <TextView
        android:layout_width="200dp"
        android:layout_column="3"
        android:layout_weight="1"
        android:text="Serialization" />

    <TextView
        android:layout_width="200dp"
        android:layout_column="4"
        android:layout_weight="1"
        android:text="Spec" />

    <TextView
        android:layout_width="200dp"
        android:layout_column="5"
        android:layout_weight="1"
        android:text="SKU" />

    <TextView
        android:layout_width="200dp"
        android:layout_column="6"
        android:layout_weight="1"
        android:text="Material Number" />
</TableRow>

<ScrollView android:layout_height="match_parent"
    android:scrollbars="horizontal|vertical" 
    android:layout_width="match_parent"
    >      

    <HorizontalScrollView 
    android:layout_height="wrap_content"     
    android:scrollbars="horizontal|vertical" 
    android:layout_width="wrap_content" >

<TableLayout android:id="@+id/tiresTable"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#C0C0C0"  >          
<TableRow>

</TableRow>
</TableLayout>
</HorizontalScrollView>
</ScrollView>     
</TableLayout>

1 个答案:

答案 0 :(得分:0)

更改您的代码,如下所示。因为您在添加后删除了所有视图。并且您没有再次初始化行的子视图。

    public void createTable(String xmlForTable) 
        throws XmlPullParserException, IOException {
    //public void createTable(){

        Integer i = 0;
        endPlacementTag = "nothing";
        //setContentView(R.layout.activity_all_tires);

         lo = (TableLayout)  findViewById(R.id.tiresTable);

         TableRow headerRow = new TableRow(this);
         TableRow.LayoutParams hp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
         headerRow.setLayoutParams(hp);

         tv = new TextView(this);
         tv.setText("Text Header");
         headerRow.addView(tv);
         lo.addView(headerRow);

         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput(new StringReader (xmlForTable));
         int eventType = xpp.getEventType();


            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);





         while (eventType != XmlPullParser.END_DOCUMENT) {
               TableRow row= new TableRow(this);
                row.setLayoutParams(lp);
                rfid = new TextView(this);
                projectNumber = new TextView(this);
                inspectionLotNumber = new TextView(this);
                serialization = new TextView(this);
                spec = new TextView(this);
                sku = new TextView(this);
                materialNumber = new TextView(this);
            if(eventType == XmlPullParser.START_DOCUMENT) {

                  System.out.println("Start document");

              } else if(eventType == XmlPullParser.END_DOCUMENT) {

                  System.out.println("End document");

              } else if(eventType == XmlPullParser.START_TAG) {

                  System.out.println("Start tag "+xpp.getName());
                  placementTag = xpp.getName();

              } else if(eventType == XmlPullParser.END_TAG) {

                  System.out.println("End tag "+xpp.getName());
                  endPlacementTag = "End tag "+xpp.getName();

              } else if(eventType == XmlPullParser.TEXT) {

                  System.out.println("Text "+xpp.getText());
              }

               ....//filled with more if statements same as this one just different .equals

              if(placementTag.equals("MaterialNumber"))
                  {
                      materialNumber.setText(xpp.getText());
                      materialNumber.setTextSize(10);                   
                      materialNumber.setLayoutParams(new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                      //row.addView(materialNumber);
                      placementTag = "";

                  }
                  else if(endPlacementTag.equals("End tag Table")) 
                  {

                      row.addView(rfid);
                      row.addView(projectNumber);
                      row.addView(inspectionLotNumber);
                      row.addView(serialization);
                      row.addView(spec);
                      row.addView(sku);
                      row.addView(materialNumber);

                      row.setGravity(Gravity.CENTER);
                      row.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                      lo.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                      i++;

                  }

                  }
              eventType = xpp.next();
              }