【问题标题】:TableLayout binding problem at runtime?运行时的 TableLayout 绑定问题?
【发布时间】:2011-08-04 17:40:34
【问题描述】:

我有对象数组列表,我需要在运行时将这些对象列表绑定到 TableLayout,但我没有将任何数据放入其中,请检查我的代码:

XML 代码:

<TableLayout android:background="#cfe1edFF" android:id="@+id/tl"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >

  <TableRow>
    <TextView android:text="Symbol"
    android:layout_height="fill_parent" android:textColor="#104e8b"
        android:layout_width="wrap_content"
        android:layout_weight="1" android:gravity="center" android:background="@drawable/shapefile"/>
    <TextView android:text="A/c Type"
    android:layout_height="fill_parent" android:textColor="#104e8b"
        android:layout_width="wrap_content"
        android:layout_weight="1" android:gravity="center" android:background="@drawable/shapefile">
    </TextView>
    <TextView android:text="Position" android:textColor="#104e8b" android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="1" android:gravity="center" android:background="@drawable/shapefile">
    </TextView>
  </TableRow>

    <ScrollView>      
    <TableLayout android:id="@+id/score_table" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"> 
      <TableRow></TableRow>         
    </TableLayout>
  </ScrollView>     
</TableLayout>

所以从上面的 TableLayout "score_table" 我试图绑定来自 ArrayList 对象的数据,例如:

使用 arryalist 对象绑定表格布局的代码:

public void TableBinding(ArrayList<Position> posLst)
    {
        TableLayout tLayout=(TableLayout)findViewById(R.id.score_table);
        Position pos;
        for(int i=0;i<posLst.size();i++)
        {
            TableRow tr = new TableRow(this);
            tr.setId(100+i);
            pos=posLst.get(i);
            tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));


            TextView b = new TextView(this);
            b.setText(pos.Symbol); 
            tr.addView(b);
            //b.setId(200+i);

            TextView b1 = new TextView(this);
            b1.setText(pos.AcType);
            tr.addView(b1);
            //b1.setId(300+i);

            TextView b2 = new TextView(this);
            b2.setText(pos.Position);
            tr.addView(b2);
            //b2.setId(400+i);



            /* Add row to TableLayout. */
            tLayout.addView(tr,new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

        }

    }

但是我没有得到任何数据,我已经检查了 ArrayList 获取数据,请帮助我。

谢谢, 唠叨。

【问题讨论】:

    标签: android arraylist tablelayout


    【解决方案1】:

    你试试这个方法:

     ArrayList<String> tablesName = new ArrayList<String>();
     TableLayout tl;
    
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    
       tl = (TableLayout) findViewById(R.id.tableLayout1);
    
        for (int i = 0; i < posLst.size(); i++) {
        TableRow tr = new TableRow(this);
        CheckBox ch = new CheckBox(this);
        ch.setId(i);
    
        TextView tv2 = new TextView(this);
        tr.addView(ch);
        createView(tr, tv2, tablesName.get(i));
        tl.addView(tr);
    }   
    
        public void createView(TableRow tr, TextView t, String viewdata) {
        t.setText(viewdata);
        t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        t.setTextColor(Color.DKGRAY);
        t.setPadding(20, 0, 0, 0);
        tr.setPadding(0, 1, 0, 1);
        tr.addView(t); 
    }
    

    【讨论】:

      【解决方案2】:

      试试这个对我有用

      for (int position=0; position < data.size(); position++)
                  {
                      TableRow tableRow= new TableRow(this);
      
                      ArrayList<Object> row = data.get(position);
      
                      TextView idText = new TextView(this);
                      idText.setText(pos.Symbol);
                      tableRow.addView(idText);
      
                      TextView textOne = new TextView(this);
                      textOne.setText(pos.AcType);
                      tableRow.addView(textOne);
      
                      TextView textTwo = new TextView(this);
                      textTwo.setText(pos.AcType);
                      tableRow.addView(textTwo);
      
                      dataTable.addView(tableRow);     
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2013-05-28
        • 2017-07-20
        • 1970-01-01
        • 2017-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        相关资源
        最近更新 更多