【问题标题】:How to dispaly result list into TableLayout row in android?如何在android中的表格布局行中显示结果列表?
【发布时间】:2014-03-01 09:17:46
【问题描述】:

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

1, 2014-02-05, S02, 20

这是我上面场景0的代码sn-p。

<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 db。但是,我得不到我想要的。这是我用于循环列表并显示结果的代码。但是,我得到了我的一些默认值。请指出我怎样才能得到这个。谢谢。

这里 sn-p 显示列表。

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
  • 我不明白问题出在哪里,您在布局中设置了默认值,并且不会在代码中显示它们???
  • @mohammed momn 实际上,当我没有从 sqlite 获得空值时,我想显示默认值。我想动态创建这个布局。
  • 据我所知,如果光标中有数据,则需要删除第一个线性(默认)?
  • 我想,我应该试试 ListView

标签: android sqlite android-layout


【解决方案1】:

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

主布局

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>

表格行布局

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

【讨论】:

  • 是的,兄弟。你的演示代码真的很有帮助。我明白了。
【解决方案2】:

查看上下文层次结构

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

【讨论】:

  • 张贴你的照片
  • 出现在我的第一张图片中。
  • @K-THIHA 你多么期待它出现在屏幕上
【解决方案3】:

使用列表视图,您可以轻松做到这一点:

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 是标题布局

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-24
    • 2011-10-03
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多