Android 在运行时将数据加载到表格布局

问题描述 投票:0回答:1

在我的 Android 应用程序中,我必须在表格布局中显示从数据库获取的数据。我获取数据,当我将 TableRows 添加到 Table 时,它给出了一个空指针。我正确实例化了表中的元素,但我无法找出错误。

我的代码片段如下:

TableLayout tbl=(TableLayout) findViewById(R.id.dpl_tbl_lst);
        
        if(!retailersList.isEmpty()){
            
            //TableRow Hedding=(TableRow) findViewById(R.id.dpl_tbl_hdr);
            //tbl.addView(Hedding);
            
            for(int i=0;i<retailersList.size();i++){
                
                Retailer RtlIns=retailersList.get(i);
                
                
                TableRow tbl_rw=(TableRow) findViewById(R.id.dpl_tbr_cnt);
                
                TextView sequence, custommerName, city, status;
                sequence=(TextView) findViewById(R.id.dpl_txt_seq);
                custommerName=(TextView) findViewById(R.id.dpl_txt_cus_nm);
                city=(TextView) findViewById(R.id.dpl_txt_cty);
                status=(TextView) findViewById(R.id.dpl_txt_sts);
                
                sequence.setText(RtlIns.getPosition());
                custommerName.setText(RtlIns.getName());
                city.setText(RtlIns.getCity());
                status.setText(RtlIns.getStatus());
                
                tbl_rw.addView(sequence);
                tbl_rw.addView(custommerName);
                tbl_rw.addView(city);
                tbl_rw.addView(status);
                
                
                tbl.addView(tbl_rw);}

我的表格布局如下:

<TableLayout
            android:id="@+id/dpl_tbl_lst"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableRow
                android:id="@+id/dpl_tbl_hdr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textview3"
                    android:layout_width="@dimen/cell_width_0"
                    android:layout_height="@dimen/cell_h_h"
                    android:background="@drawable/cell_shape"
                    android:padding="@dimen/cell_pdn"
                    android:text="@string/list_sequense"
                    android:textColor="@color/white"
                    android:textSize="@dimen/normal_text"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="@dimen/cell_width_2"
                    android:layout_height="@dimen/cell_h_h"
                    android:background="@drawable/cell_shape"
                    android:padding="@dimen/cell_pdn"
                    android:text="@string/list_cust_name"
                    android:textColor="@color/white"
                    android:textSize="@dimen/normal_text"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="@dimen/cell_width_2"
                    android:layout_height="@dimen/cell_h_h"
                    android:background="@drawable/cell_shape"
                    android:padding="@dimen/cell_pdn"
                    android:text="@string/list_city"
                    android:textColor="@color/white"
                    android:textSize="@dimen/normal_text"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="@dimen/cell_width_1"
                    android:layout_height="@dimen/cell_h_h"
                    android:background="@drawable/cell_shape"
                    android:padding="@dimen/cell_pdn"
                    android:text="@string/list_status"
                    android:textColor="@color/white"
                    android:textSize="@dimen/normal_text"
                    android:textStyle="bold" />
            </TableRow>
            
        </TableLayout>
android android-layout tablelayout
1个回答
1
投票
// try this way i gave simple demo but you have use your data rather my static data..
1. **table.xml**
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dpl_tbl_lst"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</TableLayout>
2.**table_row.xml**
<?xml version="1.0" encoding="utf-8"?>
<TableRow  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dpl_tbl_hdr"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/dpl_txt_seq"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="asd"/>

    <TextView
        android:id="@+id/dpl_txt_cus_nm"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="asd"/>

    <TextView
        android:id="@+id/dpl_txt_cty"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="asd"/>

    <TextView
        android:id="@+id/dpl_txt_sts"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="asd"/>
</TableRow>

3. **MyActivity.java**
public class MyActivity extends Activity {
    TableLayout dpl_tbl_lst;
    @Override
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        dpl_tbl_lst = (TableLayout) findViewById(R.id.dpl_tbl_lst);

        for (int i=0;i<5;i++){
            View row = LayoutInflater.from(this).inflate(R.layout.table_row,null,false);

            TextView dpl_txt_seq = (TextView) row.findViewById(R.id.dpl_txt_seq);
            TextView dpl_txt_cus_nm = (TextView) row.findViewById(R.id.dpl_txt_cus_nm);
            TextView dpl_txt_cty = (TextView) row.findViewById(R.id.dpl_txt_cty);
            TextView dpl_txt_sts = (TextView) row.findViewById(R.id.dpl_txt_sts);

            dpl_txt_seq.setText("Seq"+String.valueOf(i+1));
            dpl_txt_cus_nm.setText("Nm"+String.valueOf(i+1));
            dpl_txt_cty.setText("City"+String.valueOf(i+1));
            dpl_txt_sts.setText("Sts"+String.valueOf(i+1));

            dpl_txt_seq.setTag("Seq"+String.valueOf(i+1));
            dpl_txt_seq.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String data = view.getTag().toString();
                    Toast.makeText(MyActivity.this,data,Toast.LENGTH_SHORT).show();

                }
            });
            dpl_txt_cus_nm.setTag("Nm"+String.valueOf(i+1));
            dpl_txt_cus_nm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String data = view.getTag().toString();
                    Toast.makeText(MyActivity.this,data,Toast.LENGTH_SHORT).show();

                }
            });
            dpl_txt_cty.setTag("City"+String.valueOf(i+1));
            dpl_txt_cty.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String data = view.getTag().toString();
                    Toast.makeText(MyActivity.this,data,Toast.LENGTH_SHORT).show();

                }
            });
            dpl_txt_sts.setTag("Sts"+String.valueOf(i+1));
            dpl_txt_sts.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String data = view.getTag().toString();
                    Toast.makeText(MyActivity.this,data,Toast.LENGTH_SHORT).show();

                }
            });
            dpl_tbl_lst.addView(row);
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.