2013-04-17 76 views
0

我在想,如果我可以添加数据的动态行的表,其布局已经定义...为前:动态添加行的表,在布局设计

<TableLayout 
     android:id="@+id/myTableOrders" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10sp"> 

      <TableRow 
       android:id="@+id/ordersRow" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 

       <TextView android:id="@+id/name" 
        android:textSize="14sp" 
        android:textStyle="bold" 
        android:layout_marginLeft="10sp" 
        android:layout_marginTop="5sp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.6"    
       />          

       <TextView android:id="@+id/quantity" 
        android:textSize="14sp" 
        android:textStyle="bold" 
        android:layout_marginTop="5sp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.2" 
       /> 

       <TextView android:id="@+id/total_price" 
        android:textSize="14sp" 
        android:textStyle="bold" 
        android:layout_marginTop="5sp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.2" 
       /> 
      </TableRow> 
</TableLayout> 

考虑这个表,其中每行都有3个文本浏览,这个样式...我现在可以给每个文本浏览提供数据吗?或者我也应该在处理总体的Java代码中编写样式?

如果yes..maybe你可以给我一个提示:)

编辑:这编程方式使用以下

TableLayout ordersTable = (TableLayout)findViewById(R.id.myTableOrders); 
    for (int i=0;i<products.size();i++){ 

     //Create a new row to be added. 
     TableRow tr = new TableRow(this); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 

     //Create text views to be added to the row. 
     TextView name = new TextView(this); 
     name.setText(products.get(i).getName()); 
     name.setTextSize(16); 
     name.setTypeface(Typeface.DEFAULT_BOLD); 
     TableRow.LayoutParams nameParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.6f); 
     nameParams.setMargins(7, 5, 0, 0); 

     TextView quantity = new TextView(this); 
     quantity.setText(products.get(i).getQuantityString()); 
     quantity.setTextSize(16); 
     quantity.setTypeface(Typeface.DEFAULT_BOLD); 
     TableRow.LayoutParams quantityParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.2f); 
     quantityParams.setMargins(0, 5, 0, 0); 

     TextView totalPrice = new TextView(this); 
     totalPrice.setTextSize(16); 
     totalPrice.setTypeface(Typeface.DEFAULT_BOLD); 
     TableRow.LayoutParams totalPriceParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.2f); 
     totalPriceParams.setMargins(0, 5, 0, 0); 

     tr.addView(name,nameParams); 
     tr.addView(quantity,quantityParams); 
     tr.addView(totalPrice,totalPriceParams); 

     ordersTable.addView(tr); 
+0

将数据提供给textViews意味着什么?你的意思是为每个人设置文字吗? –

+0

是的......我的意思是我不想搜索和编写边缘,重量,颜色等的java代码......如果可能的话 – Teshte

+0

如果您只是想为这些已定义的TextViews设置文本,请在java代码,你可以使用'setText(“text”)' –

回答

0

的代码在你的活动解决了,使用类似

TextView tv = (TextView) findViewById(R.id.name); //do this for all text views : retrieve them using Ids. 
    tv.setText("someText") //someText = text that you want to set 
+0

这应该是R.id.name而不是R.id. @ + ID /名称' – Squonk

+1

感谢您指出:),我的坏! –

+0

恐怕没有用......我认为这些ID必须是独一无二的? – Teshte