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

4

我在Android中显示列表数据到TextView方面遇到了一点问题。我的场景是有一个默认为一个TableRow的TableLayout。在表格行内,我创建了一个新的LinearLayout,然后再里面创建了四个TextView。我给这些textview添加了一些默认值。我的默认值是:

1, 2014-02-05, S02, 20

以下是上述场景的代码片段。

<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>

输入图像描述

实际上,我想动态创建 TableRowLinearLayoutTextView。然后,我将列表结果添加到这些中。列表结果来自sqlite数据库。但是,我得不到我想要的。以下是我的代码循环遍历列表并显示结果。但是,我得到了一些默认值。请指出如何获取此内容。谢谢。

以下是用于显示列表的代码片段:

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

我建议您使用 ListView 来完成这个任务。 - Chintan Soni
我不明白问题出在哪里,你在布局中设置了默认值,但在代码中却没有显示??? - mohammed momn
@mohammed momn 实际上,当我从sqlite中获取到非空值时,我想显示默认值。我想动态创建这个布局。 - 502_Geek
所以我理解的是,如果你在游标中有数据,需要先删除第一个线性(默认)? - mohammed momn
我认为,我应该尝试使用ListView。 - 502_Geek
3个回答

6

请尝试这个方法,这里给出了示例或演示代码,您需要根据自己的要求进行修改,如果仍然有问题,请告诉我。

主要布局

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

是的兄弟。你的演示代码真的很有帮助。我明白了。 - 502_Geek

1
请查看上下文层次结构。
for(IspHistoryListObject hl : historyList) {
    count++;
    TableRow tr = new TableRow(showRow.getContext());
    LinearLayout ll = new LinearLayout(tr.getContext());
    ll.setLayoutParams(new LinearLayout.LayoutParams(50,30));
    TextView tv_sync_no = new TextView(ll.getContext());
    TextView tv_sync_date = new TextView(ll.getContext());
    TextView tv_sync_orderid = new TextView(ll.getContext());
    TextView tv_sync_qty = new TextView(ll.getContext());
    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);
}

发布图片展示你的获取方式。 - Sush
出现在我的第一张图片中。 - 502_Geek
@K-THIHA 你期望它如何在屏幕上显示? - Sush

0
使用ListView,您可以轻松地完成这个任务:

ActivityMain.java

ListView lst = (ListView) findViewById(R.id.listView);
View header = getLayoutInflater().inflate(R.layout.list_header, null);
lst.addHeaderView(header);
lst.setAdapter(new CustomerListAdapter(this,4));

list_header.xml 是头部布局


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接