运行时的TableLayout绑定问题?

时间:2011-08-04 09:12:09

标签: android arraylist tablelayout

我是对象的arraylist,我需要在运行时将这些对象列表绑定到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获取数据,请帮助我。

谢谢, NAG。

2 个答案:

答案 0 :(得分: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); 
}

答案 1 :(得分:0)

试试这对我有用

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);     
            }
        }
相关问题