如何将结果列表分配到Android中的TableLayout行中? [英] How to dispaly result list into TableLayout row in android?
问题描述
我在将列表数据显示到Android中的TextView时遇到了一点问题.我的情况是我有一个 TableLayout 和一个默认的 TableRow .在表格行中,我已经创建了新的 LinearLayout .然后,在 LinearLayout 中创建了四个 TextView .我在此textview中添加了一些默认值.我的默认值为
I have a little bit problem that displaying list data to TextView in Android. My scenario is I have a TableLayout with Default One TableRow. Inside table row, I has been created new LinearLayout. Then, Four TextView created inside LinearLayout. I adding some default value to this textview. My default value is
1,2014-02-05,S02,20
1, 2014-02-05, S02, 20
这是我上面scenari0的代码段.
Here my code snippet for above scenari0.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/sync_history_table_color"
android:id="@+id/history_table"
android:orientation="vertical"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="1dp"
android:id="@+id/row_start">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="30dp" android:id="@+id/row_linear" android:layout_weight="1"
android:background="@color/isp_home_color">
<TextView android:layout_width="0dp" android:layout_height="match_parent" android:text="@string/sync_default_no"
android:id="@+id/history_display_no" android:layout_weight="0.165"
android:gravity="center_vertical|left" android:paddingLeft="10dp"
android:textColor="@color/sync_history_text_color"/>
<TextView android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/sync_default_date"
android:id="@+id/history_display_date"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/sync_default_order"
android:id="@+id/history_display_orderid"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/sync_history_text_color"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/sync_default_qty"
android:id="@+id/history_display_quantity" android:layout_weight="0.5" android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/sync_history_text_color"/>
</LinearLayout>
</TableRow>
</TableLayout>
实际上,我想动态创建 TableRow , LinearLayout 和 TextView .然后,我将列表结果添加到其中.列表结果来自sqlite db.但是,我无法获得想要的东西.在这里,我的代码用于循环列表并显示结果.但是,我得到了一些默认值.请指点我如何获得这个.谢谢.
Actually, I want to create dynamically TableRow, LinearLayout and TextView. Then, I put the list result to add these. The list result come from sqlite db. But, I can't get what I want. Here my code for looping the list and display the result. But, I got my some default value. Please pointing to me how can I get this. Thanks.
此处为显示列表的摘要.
Here snippet for display list.
public void displayHistory(){
List<IspHistoryListObject> historyList = sqlaccess.getAllItems();
TableLayout showRow = (TableLayout) findViewById(R.id.history_table);
int count = 0;
for(IspHistoryListObject hl : historyList) {
count++;
TableRow tr = new TableRow(this);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
TextView tv_sync_no = new TextView(this);
TextView tv_sync_date = new TextView(this);
TextView tv_sync_orderid = new TextView(this);
TextView tv_sync_qty = new TextView(this);
tv_sync_no.setText(String.valueOf(count));
tv_sync_date.setText(hl.getSyncDate().substring(0,10));
tv_sync_orderid.setText(hl.getSyncOrderIdRef());
tv_sync_qty.setText(String.valueOf(hl.getQty()));
ll.addView(tv_sync_no);
ll.addView(tv_sync_date);
ll.addView(tv_sync_orderid);
ll.addView(tv_sync_qty);
tr.addView(ll);
showRow.addView(tr);
}
}
推荐答案
在这里尝试这种方式,我给出了示例代码或演示代码,您必须根据自己的要求进行修改,但仍然有问题让我知道
try this way here I gave sample or demo code you have to modify as per your requirement and still have problem let me know
主布局
1. table.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
</TableLayout>
表行布局
2. table_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/history_display_no"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/history_display_date"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/history_display_orderid"
android:layout_weight="0.25"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/history_display_quantity"
android:layout_weight="0.25"
android:gravity="center"/>
</TableRow>
活动课程
3.活动
public class MyActivity extends Activity {
private TableLayout tableLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(TableLayout)findViewById(R.id.tableLayout);
for (int i=0;i<5;i++){
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item,null,false);
TextView history_display_no = (TextView) tableRow.findViewById(R.id.history_display_no);
TextView history_display_date = (TextView) tableRow.findViewById(R.id.history_display_date);
TextView history_display_orderid = (TextView) tableRow.findViewById(R.id.history_display_orderid);
TextView history_display_quantity = (TextView) tableRow.findViewById(R.id.history_display_quantity);
history_display_no.setText(""+(i+1));
history_display_date.setText("2014-02-05");
history_display_orderid.setText("S0"+(i+1));
history_display_quantity.setText(""+(20+(i+1)));
tableLayout.addView(tableRow);
}
}
}
这篇关于如何将结果列表分配到Android中的TableLayout行中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!