Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中将结果列表显示到TableLayout行中?_Android_Sqlite_Android Layout - Fatal编程技术网

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

如何在android中将结果列表显示到TableLayout行中?,android,sqlite,android-layout,Android,Sqlite,Android Layout,我有一个小问题,就是在Android中向TextView显示列表数据。我的场景是我有一个表格布局,默认为表格行。在表行中,我已被创建为新的LinearLayout。然后,在LinearLayout内部创建四个TextView。我正在为这个文本视图添加一些默认值。我的默认值是 12014-02-05,S02,20 这里是我的上述场景0的代码片段 <TableLayout android:layout_width="fill_parent" android:la

我有一个小问题,就是在Android中向TextView显示列表数据。我的场景是我有一个表格布局,默认为表格行。在表行中,我已被创建为新的LinearLayout。然后,在LinearLayout内部创建四个TextView。我正在为这个文本视图添加一些默认值。我的默认值是

12014-02-05,S02,20

这里是我的上述场景0的代码片段

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

实际上,我想动态创建表格行线性布局文本视图。然后,我将列表结果添加到这些。列表结果来自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);
    }
}
public void displayHistory(){
List historyList=sqlaccess.getAllItems();
TableLayout showRow=(TableLayout)findViewById(R.id.history_table);
整数计数=0;
for(IspHistoryListObject hl:historyList){
计数++;
TableRow tr=新的TableRow(本);
LinearLayout ll=新的LinearLayout(本);
ll.setLayoutParams(新的LinearLayout.LayoutParams(50,30));
TextView tv_sync_no=新的TextView(此);
TextView tv\u sync\u date=新的TextView(此);
TextView tv\u sync\u orderid=新的TextView(此);
TextView电视同步数量=新TextView(此);
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(电视同步号);
ll.addView(电视同步日期);
ll.addView(电视同步订单id);
ll.addView(电视同步数量);
tr.addView(ll);
showRow.addView(tr);
}
}

检查上下文层次结构

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

尝试这种方式在这里,我给了样品或演示代码,您必须根据您的要求修改,仍然有问题让我知道

主布局

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

表格行布局

2。表_item.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>
<?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);
        }
    }
}
公共类MyActivity扩展活动{
私人桌面布局;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
tableLayout=(tableLayout)findviewbyd(R.id.tableLayout);

对于(int i=0;i,使用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是header布局

我建议您使用
ListView
。我不知道问题出在哪里,您在布局中设置了默认值,不会在代码中显示它们???@mohammed momn实际上,当我从sqlite中没有获取空值时,我想显示默认值。我想动态创建此布局。因此,我理解如果光标中有数据,是否需要删除第一个线性(默认值)?我想,我应该尝试使用ListViewpost图像来显示您在我的第一个图像中的表现。@K-THIHA您希望它出现在屏幕上是的,兄弟。您的演示代码非常有用。我得到了这个。