动态表不会添加任何行

时间:2014-08-05 19:05:43

标签: android android-tablelayout

我有一个动态表格,我试图在屏幕上显示。它是通过解析从Web服务返回的xml字符串创建的。但是,它运行代码没有错误,但它不会为我添加任何行...

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


    Integer i = 0;
    endPlacementTag = "nothing";


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

     TableRow headerRow = new TableRow(this);
     TableRow.LayoutParams hp = new TableRow.LayoutParams(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);
        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());




              //If the text tag is equal to "Table" do nothing otherwise...
              if(placementTag.equals("Table"))
              {
                  //Do Nothing
              }

              else if(placementTag.equals("Serialization"))
              {
                  serialization.setText(xpp.getText());
                  serialization.setTextSize(10);
                  serialization.setLayoutParams(new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                  //row.addView(serialization);
                  placementTag = "";

              }

             ... //Other if statements

              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.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

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

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

              }

              }
          eventType = xpp.next();
          }

我设置了类似于我在SO上的其他示例中读过的参数,我确保我已导入import android.widget.TableRow.LayoutParams;而不是import android.view.ViewGroup.LayoutParams;

有人有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

我认为上面的构造函数不是你想要的。以上是

public TableRow.LayoutParams (int column)
Puts the view in the specified column.
Sets the child width to MATCH_PARENT and the child height to WRAP_CONTENT.

你可能想要使用这个:

public TableRow.LayoutParams (int w, int h)
Sets the child width and height.

像这样:

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