如何在android中将结果列表显示到TableLayout行中?

502*_*eek 4 sqlite android android-layout

我在 Android 中将列表数据显示到 TextView 时遇到了一点问题。我的情况是我有一个TableLayout,默认为 One TableRow。在表格行内,我创建了新的LinearLayout。然后,在LinearLayout 中创建了四个TextView。我向这个文本视图添加了一些默认值。我的默认值是

1, 2014-02-05, S02, 20

这是我上面 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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

实际上,我想动态创建TableRowLinearLayoutTextView。然后,我把列表结果添加到这些。列表结果来自 sqlite db。但是,我不能得到我想要的。这是我用于循环列表并显示结果的代码。但是,我得到了一些默认值。请指点我怎么才能得到这个。谢谢。

这里是显示列表的片段。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Har*_*ana 5

在这里尝试这种方式我提供了示例或演示代码,您必须根据您的要求进行修改,但仍有问题请告诉我

主要布局

1.表.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>
Run Code Online (Sandbox Code Playgroud)

表格行布局

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>
Run Code Online (Sandbox Code Playgroud)

活动课

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)