繁体   English   中英

TableLayout在运行时绑定问题?

[英]TableLayout binding problem at runtime?

我已经有了对象的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获取数据,请帮助我。

谢谢,g。

您可以这样尝试:

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

试试这个对我有用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM